|
|
|
|
@@ -19,40 +19,115 @@ function makeUrl(params: Record<string, string | string[]> = {}) {
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── happy path ───────────────────────────────────────────────────────────────
|
|
|
|
|
// ─── dashboard mode (no search filters) ──────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
describe('home page load — happy path', () => {
|
|
|
|
|
it('returns documents and persons on success', async () => {
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({
|
|
|
|
|
GET: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
|
response: { ok: true, status: 200 },
|
|
|
|
|
data: [{ id: 'd1', title: 'Brief' }]
|
|
|
|
|
})
|
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
|
response: { ok: true, status: 200 },
|
|
|
|
|
data: [{ id: 'p1', firstName: 'Hans', lastName: 'Müller' }]
|
|
|
|
|
})
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: { count: 3 } })
|
|
|
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
|
describe('home page load — dashboard mode', () => {
|
|
|
|
|
it('sets isDashboard true and fetches all three widget APIs', async () => {
|
|
|
|
|
const mockGet = vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) // persons
|
|
|
|
|
.mockResolvedValueOnce({
|
|
|
|
|
response: { ok: true },
|
|
|
|
|
data: { content: [{ id: 'n1' }] }
|
|
|
|
|
}) // notifications
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [{ id: 'd1' }] }) // incomplete
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [{ id: 'd2' }] }); // recent
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType<
|
|
|
|
|
typeof createApiClient
|
|
|
|
|
>);
|
|
|
|
|
|
|
|
|
|
const result = await load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch });
|
|
|
|
|
|
|
|
|
|
expect(result.documents).toHaveLength(1);
|
|
|
|
|
expect(result.incompleteCount).toBe(3);
|
|
|
|
|
expect(result.error).toBeNull();
|
|
|
|
|
expect(result.isDashboard).toBe(true);
|
|
|
|
|
expect(result.mentions).toHaveLength(1);
|
|
|
|
|
expect(result.incompleteDocs).toHaveLength(1);
|
|
|
|
|
expect(result.recentDocs).toHaveLength(1);
|
|
|
|
|
expect(result.documents).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('passes search params from the URL to the API', async () => {
|
|
|
|
|
it('defaults mentions to [] when notifications API rejects', async () => {
|
|
|
|
|
const mockGet = vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) // persons
|
|
|
|
|
.mockRejectedValueOnce(new Error('network')) // notifications
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] }) // incomplete
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] }); // recent
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType<
|
|
|
|
|
typeof createApiClient
|
|
|
|
|
>);
|
|
|
|
|
|
|
|
|
|
const result = await load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch });
|
|
|
|
|
|
|
|
|
|
expect(result.mentions).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('defaults incompleteDocs to [] when incomplete API rejects', async () => {
|
|
|
|
|
const mockGet = vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) // persons
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: { content: [] } }) // notifications
|
|
|
|
|
.mockRejectedValueOnce(new Error('network')) // incomplete
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] }); // recent
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType<
|
|
|
|
|
typeof createApiClient
|
|
|
|
|
>);
|
|
|
|
|
|
|
|
|
|
const result = await load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch });
|
|
|
|
|
|
|
|
|
|
expect(result.incompleteDocs).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('defaults recentDocs to [] when recent-activity API rejects', async () => {
|
|
|
|
|
const mockGet = vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) // persons
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: { content: [] } }) // notifications
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] }) // incomplete
|
|
|
|
|
.mockRejectedValueOnce(new Error('network')); // recent
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType<
|
|
|
|
|
typeof createApiClient
|
|
|
|
|
>);
|
|
|
|
|
|
|
|
|
|
const result = await load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch });
|
|
|
|
|
|
|
|
|
|
expect(result.recentDocs).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ─── search mode (any filter active) ─────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
describe('home page load — search mode', () => {
|
|
|
|
|
it('sets isDashboard false and skips widget APIs when q is set', async () => {
|
|
|
|
|
const mockGet = vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [{ id: 'd1' }] }) // search docs
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }); // persons
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType<
|
|
|
|
|
typeof createApiClient
|
|
|
|
|
>);
|
|
|
|
|
|
|
|
|
|
const result = await load({
|
|
|
|
|
url: makeUrl({ q: 'Urlaub' }),
|
|
|
|
|
fetch: vi.fn() as unknown as typeof fetch
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.isDashboard).toBe(false);
|
|
|
|
|
expect(result.documents).toHaveLength(1);
|
|
|
|
|
expect(result.mentions).toEqual([]);
|
|
|
|
|
expect(result.incompleteDocs).toEqual([]);
|
|
|
|
|
expect(result.recentDocs).toEqual([]);
|
|
|
|
|
// Only two API calls — no widget calls
|
|
|
|
|
expect(mockGet).toHaveBeenCalledTimes(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('passes search params from the URL to the documents API', async () => {
|
|
|
|
|
const mockGet = vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] })
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] })
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: { count: 0 } });
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({
|
|
|
|
|
GET: mockGet
|
|
|
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] });
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType<
|
|
|
|
|
typeof createApiClient
|
|
|
|
|
>);
|
|
|
|
|
|
|
|
|
|
await load({
|
|
|
|
|
url: makeUrl({ q: 'Urlaub', from: '2020-01-01' }),
|
|
|
|
|
@@ -63,46 +138,14 @@ describe('home page load — happy path', () => {
|
|
|
|
|
expect(firstCall[1].params.query.q).toBe('Urlaub');
|
|
|
|
|
expect(firstCall[1].params.query.from).toBe('2020-01-01');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns incompleteCount 0 when the incomplete-count API fails', async () => {
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({
|
|
|
|
|
GET: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] })
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] })
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: false }, data: null })
|
|
|
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
|
|
|
|
|
|
const result = await load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch });
|
|
|
|
|
|
|
|
|
|
expect(result.incompleteCount).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ─── 401 redirect ─────────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
describe('home page load — auth redirect', () => {
|
|
|
|
|
it('redirects to /login when documents API returns 401', async () => {
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({
|
|
|
|
|
GET: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: false, status: 401 }, data: null })
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] })
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: { count: 0 } })
|
|
|
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch })
|
|
|
|
|
).rejects.toMatchObject({ location: '/login' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('redirects to /login when persons API returns 401', async () => {
|
|
|
|
|
vi.mocked(createApiClient).mockReturnValue({
|
|
|
|
|
GET: vi
|
|
|
|
|
.fn()
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] })
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: false, status: 401 }, data: null })
|
|
|
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: { count: 0 } })
|
|
|
|
|
GET: vi.fn().mockResolvedValueOnce({ response: { ok: false, status: 401 }, data: null })
|
|
|
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
@@ -123,6 +166,5 @@ describe('home page load — network error fallback', () => {
|
|
|
|
|
|
|
|
|
|
expect(result.error).toBe('Daten konnten nicht geladen werden.');
|
|
|
|
|
expect(result.documents).toEqual([]);
|
|
|
|
|
expect(result.incompleteCount).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|