245b76ec43
ESLint emitted 17 errors in tests/Frontend/* (production code clean):
- 13× @typescript-eslint/no-explicit-any in axios mock casts
(BulkActionsBar, ProjectsView, projectsStore specs)
- 3× vitest/no-disabled-tests rule-not-found
(eslint-plugin-vitest not registered; inline-disable comments stale)
- 1× @typescript-eslint/no-unused-vars on imported beforeEach
Plus Phase 5 audit finding: TwoFactorView.spec.ts test router was
missing /recovery-use stub → Vue Router warn on every TwoFactorView mount.
Changes:
- BulkActionsBar.spec.ts, ProjectsView.spec.ts, projectsStore.spec.ts:
replace `as any` with `as unknown as ReturnType<typeof vi.fn>` on
axios method mocks; one case used `as unknown as { regionsOpen: bool }`
for vm shape.
- NewProjectDialog.spec.ts, ProjectsView.spec.ts: remove stale
`// eslint-disable-next-line vitest/no-disabled-tests` comments
(it.skip() lines kept).
- ProjectsView.toolbar.spec.ts: drop unused `beforeEach` from import.
- TwoFactorView.spec.ts: add `/recovery-use` route stub.
Verification:
- npx eslint --max-warnings=0 → exit 0 (was 17 errors).
- npx vitest run on affected specs → 24/27 passed + 3 skipped (was same).
- TwoFactorView spec → 3/3 passed, no Vue Router warn.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
167 lines
6.0 KiB
TypeScript
167 lines
6.0 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
import { setActivePinia, createPinia } from 'pinia';
|
|
import axios from 'axios';
|
|
|
|
vi.mock('axios');
|
|
|
|
import { useProjectsStore } from '../../resources/js/stores/projectsStore';
|
|
|
|
describe('projectsStore (no polling)', () => {
|
|
beforeEach(() => {
|
|
setActivePinia(createPinia());
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('fetches and stores list', async () => {
|
|
(axios.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
data: { data: [{ id: 1, name: 'X' }], meta: { total: 1, current_page: 1, per_page: 20 } },
|
|
});
|
|
const store = useProjectsStore();
|
|
await store.fetch();
|
|
expect(store.items.length).toBe(1);
|
|
expect(store.total).toBe(1);
|
|
});
|
|
|
|
it('passes filters to API', async () => {
|
|
(axios.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
data: { data: [], meta: { total: 0, current_page: 1, per_page: 20 } },
|
|
});
|
|
const store = useProjectsStore();
|
|
store.filters.signal_type = 'site';
|
|
store.filters.search = 'okna';
|
|
await store.fetch();
|
|
expect(axios.get).toHaveBeenCalledWith(
|
|
'/api/projects',
|
|
expect.objectContaining({
|
|
params: expect.objectContaining({ signal_type: 'site', search: 'okna' }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('create dispatches POST + refetches', async () => {
|
|
(axios.post as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
data: { data: { id: 2, name: 'New' } },
|
|
});
|
|
(axios.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
data: { data: [{ id: 2 }], meta: { total: 1, current_page: 1, per_page: 20 } },
|
|
});
|
|
const store = useProjectsStore();
|
|
await store.create({
|
|
name: 'New',
|
|
signal_type: 'site',
|
|
signal_identifier: 'x.ru',
|
|
daily_limit_target: 10,
|
|
region_mask: 0,
|
|
region_mode: 'include',
|
|
delivery_days_mask: 127,
|
|
});
|
|
expect(axios.post).toHaveBeenCalled();
|
|
});
|
|
|
|
it('toggleSelect adds and removes ids from selectedIds', () => {
|
|
const store = useProjectsStore();
|
|
store.toggleSelect(1);
|
|
expect(store.selectedIds.has(1)).toBe(true);
|
|
store.toggleSelect(1);
|
|
expect(store.selectedIds.has(1)).toBe(false);
|
|
});
|
|
|
|
it('bulkAction sends array of ids and clears selection', async () => {
|
|
(axios.post as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({ data: { updated: 2 } });
|
|
(axios.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
data: { data: [], meta: { total: 0, current_page: 1, per_page: 20 } },
|
|
});
|
|
const store = useProjectsStore();
|
|
store.selectedIds.add(1);
|
|
store.selectedIds.add(2);
|
|
await store.bulkAction('pause');
|
|
expect(axios.post).toHaveBeenCalledWith('/api/projects/bulk', { action: 'pause', ids: [1, 2] });
|
|
expect(store.selectedIds.size).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('projectsStore (polling)', () => {
|
|
beforeEach(() => {
|
|
setActivePinia(createPinia());
|
|
vi.useFakeTimers();
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('starts polling when pendingIds has items', async () => {
|
|
(axios.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
data: {
|
|
data: [
|
|
{
|
|
id: 1,
|
|
sync_status: 'pending',
|
|
name: 'X',
|
|
signal_type: 'site',
|
|
signal_identifier: 'x.ru',
|
|
daily_limit_target: 10,
|
|
delivered_today: 0,
|
|
is_active: true,
|
|
archived_at: null,
|
|
region_mask: 0,
|
|
region_mode: 'include',
|
|
delivery_days_mask: 127,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
const store = useProjectsStore();
|
|
store.pendingIds.add(1);
|
|
store.startPolling();
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
await vi.advanceTimersByTimeAsync(5000);
|
|
expect(axios.get).toHaveBeenCalled();
|
|
});
|
|
|
|
it('stops polling when pendingIds becomes empty (sync_status ok)', async () => {
|
|
(axios.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
data: {
|
|
data: [
|
|
{
|
|
id: 1,
|
|
sync_status: 'ok',
|
|
name: 'X',
|
|
signal_type: 'site',
|
|
signal_identifier: 'x.ru',
|
|
daily_limit_target: 10,
|
|
delivered_today: 0,
|
|
is_active: true,
|
|
archived_at: null,
|
|
region_mask: 0,
|
|
region_mode: 'include',
|
|
delivery_days_mask: 127,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
const store = useProjectsStore();
|
|
store.pendingIds.add(1);
|
|
store.startPolling();
|
|
await vi.advanceTimersByTimeAsync(5000);
|
|
expect(store.pendingIds.size).toBe(0);
|
|
vi.clearAllMocks();
|
|
await vi.advanceTimersByTimeAsync(5000);
|
|
expect(axios.get).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('exponential backoff on error', async () => {
|
|
(axios.get as unknown as ReturnType<typeof vi.fn>).mockRejectedValue(new Error('network'));
|
|
const store = useProjectsStore();
|
|
store.pendingIds.add(1);
|
|
store.startPolling();
|
|
await vi.advanceTimersByTimeAsync(5000);
|
|
expect(axios.get).toHaveBeenCalledTimes(1);
|
|
await vi.advanceTimersByTimeAsync(10000);
|
|
expect(axios.get).toHaveBeenCalledTimes(2);
|
|
await vi.advanceTimersByTimeAsync(20000);
|
|
expect(axios.get).toHaveBeenCalledTimes(3);
|
|
});
|
|
});
|