Files
gochat/frontend/app/javascript/dashboard/routes/index.spec.js
T
rogee 321c61aaae Vendor Chatwoot Vue 3 frontend into frontend/
Copy the Chatwoot (v4.14.0) frontend runnable subset into frontend/ for
customization:
- app/javascript/ (Vue SPA: dashboard, widget, sdk, portal, superadmin)
- app/views/ (ERB templates for vite-plugin-ruby entrypoint resolution)
- app/helpers/, app/assets/ (Rails view helpers, static assets)
- enterprise/ (Enterprise edition frontend overlay)
- config/vite.json, vite.config.ts, bin/vite (Vite-Rails toolchain)
- package.json, pnpm-lock.yaml, tailwind/postcss/eslint configs
- Gemfile, Gemfile.lock (vite_rails gem for bin/vite binstub)

Excluded Rails backend: controllers, models, services, jobs, mailers,
policies, db, lib, spec, public, node_modules.

Update references to the new frontend location:
- .gitignore: exclude frontend build artifacts (node_modules, tmp, packs),
  keep frontend/bin/ and frontend/vendor/ via negation
- backend/scripts/parity_frontend_smoke.sh: CHATWOOT_DIR default
  reference/chatwoot -> ../frontend
- backend/scripts/parity_frontend_browser_smoke.mjs: same default update
- AGENTS.md: add frontend section with Rails/Vite coupling notes
- README: architecture tree includes frontend/
2026-07-07 14:56:01 +08:00

109 lines
2.8 KiB
JavaScript

import { validateAuthenticateRoutePermission } from './index';
import store from '../store'; // This import will be mocked
import { vi } from 'vitest';
// Mock the store module
vi.mock('../store', () => ({
default: {
getters: {
isLoggedIn: false,
getCurrentUser: {
account_id: null,
id: null,
accounts: [],
},
'accounts/getAccount': () => ({}),
},
dispatch: vi.fn(() => Promise.resolve()),
},
}));
describe('#validateAuthenticateRoutePermission', () => {
let next;
beforeEach(() => {
next = vi.fn(); // Mock the next function
});
describe('when user is not logged in', () => {
it('should redirect to login', () => {
const to = { name: 'some-protected-route', params: { accountId: 1 } };
// Mock the store to simulate user not logged in
store.getters.isLoggedIn = false;
// Mock window.location.assign
const mockAssign = vi.fn();
delete window.location;
window.location = { assign: mockAssign };
validateAuthenticateRoutePermission(to, next);
expect(mockAssign).toHaveBeenCalledWith('/app/login');
});
});
describe('when user is logged in', () => {
beforeEach(() => {
// Mock the store's getter for a logged-in user
store.getters.isLoggedIn = true;
store.getters.getCurrentUser = {
account_id: 1,
id: 1,
accounts: [
{
id: 1,
role: 'agent',
permissions: ['agent'],
status: 'active',
},
],
};
});
describe('when route is not accessible to current user', () => {
it('should redirect to dashboard', async () => {
const to = {
name: 'general_settings_index',
params: { accountId: 1 },
meta: { permissions: ['administrator'] },
};
await validateAuthenticateRoutePermission(to, next);
expect(next).toHaveBeenCalledWith('/app/accounts/1/dashboard');
});
});
describe('when route is accessible to current user', () => {
beforeEach(() => {
// Adjust store getters to reflect the user has admin permissions
store.getters.getCurrentUser = {
account_id: 1,
id: 1,
accounts: [
{
id: 1,
role: 'administrator',
permissions: ['administrator'],
status: 'active',
},
],
};
});
it('should go to the intended route', async () => {
const to = {
name: 'general_settings_index',
params: { accountId: 1 },
meta: { permissions: ['administrator'] },
};
await validateAuthenticateRoutePermission(to, next);
expect(next).toHaveBeenCalledWith();
});
});
});
});