Skip to content

Commit 0a967b1

Browse files
committed
Enabled type checking in unit tests folder
1 parent bf93e11 commit 0a967b1

23 files changed

+384
-227
lines changed

__tests__/Paginator.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ import Paginator from '../src/lib/components/common/Paginator.svelte';
66
describe('Paginator', () => {
77
it('display without ellipsis', () => {
88
const result = render(Paginator, {
9-
props: { pageSize: 10, totalCount: 70, currentPage: 4, onPageChange: () => {} }
9+
props: { pageSize: 10, totalCount: 70, currentPage: 4, onPageChange: vi.fn() }
1010
});
1111
expect(getPageItems(result)).toEqual(['«', '1', '2', '3', '4', '5', '6', '7', '»']);
1212
});
1313
it('display ellipsis at beginning', () => {
1414
const result = render(Paginator, {
15-
props: { pageSize: 10, totalCount: 1000, currentPage: 99, onPageChange: () => {} }
15+
props: { pageSize: 10, totalCount: 1000, currentPage: 99, onPageChange: vi.fn() }
1616
});
1717
expect(getPageItems(result)).toEqual(['«', '1', '...', '96', '97', '98', '99', '100', '»']);
1818
});
1919
it('display ellipsis at end', () => {
2020
const result = render(Paginator, {
21-
props: { pageSize: 10, totalCount: 1000, currentPage: 2, onPageChange: () => {} }
21+
props: { pageSize: 10, totalCount: 1000, currentPage: 2, onPageChange: vi.fn() }
2222
});
2323
expect(getPageItems(result)).toEqual(['«', '1', '2', '3', '4', '5', '...', '100', '»']);
2424
});
2525
it('display ellipsis both at beginning and end', () => {
2626
const result = render(Paginator, {
27-
props: { pageSize: 10, totalCount: 1000, currentPage: 50, onPageChange: () => {} }
27+
props: { pageSize: 10, totalCount: 1000, currentPage: 50, onPageChange: vi.fn() }
2828
});
2929
expect(getPageItems(result)).toEqual([
3030
'«',
@@ -44,13 +44,13 @@ describe('Paginator', () => {
4444
});
4545
it('omit ellipsis at end bewteen consecutive numbers', () => {
4646
const result = render(Paginator, {
47-
props: { pageSize: 10, totalCount: 70, currentPage: 3, onPageChange: () => {} }
47+
props: { pageSize: 10, totalCount: 70, currentPage: 3, onPageChange: vi.fn() }
4848
});
4949
expect(getPageItems(result)).toEqual(['«', '1', '2', '3', '4', '5', '6', '7', '»']);
5050
});
5151
it('omit ellipsis at begninning bewteen consecutive numbers', () => {
5252
const result = render(Paginator, {
53-
props: { pageSize: 10, totalCount: 70, currentPage: 5, onPageChange: () => {} }
53+
props: { pageSize: 10, totalCount: 70, currentPage: 5, onPageChange: vi.fn() }
5454
});
5555
expect(getPageItems(result)).toEqual(['«', '1', '2', '3', '4', '5', '6', '7', '»']);
5656
});
@@ -81,29 +81,29 @@ describe('Paginator', () => {
8181
});
8282
it('hide pages if total results is zero', async () => {
8383
const result = render(Paginator, {
84-
props: { pageSize: 10, totalCount: 0, currentPage: 3, onPageChange: () => {} }
84+
props: { pageSize: 10, totalCount: 0, currentPage: 3, onPageChange: vi.fn() }
8585
});
8686
expect(result.queryByLabelText('Previous')).toBeNull();
8787
});
8888
it('previous is disabled on first page', async () => {
8989
const result = render(Paginator, {
90-
props: { pageSize: 10, totalCount: 13, currentPage: 1, onPageChange: () => {} }
90+
props: { pageSize: 10, totalCount: 13, currentPage: 1, onPageChange: vi.fn() }
9191
});
9292
expect(getPageItems(result)).toEqual(['«', '1', '2', '»']);
93-
expect(result.queryByLabelText('Previous').disabled).true;
93+
expect(/** @type {HTMLLinkElement} */ (result.queryByLabelText('Previous')).disabled).true;
9494
});
9595
it('next is disabled on last page', async () => {
9696
const result = render(Paginator, {
97-
props: { pageSize: 10, totalCount: 13, currentPage: 2, onPageChange: () => {} }
97+
props: { pageSize: 10, totalCount: 13, currentPage: 2, onPageChange: vi.fn() }
9898
});
9999
expect(getPageItems(result)).toEqual(['«', '1', '2', '»']);
100-
expect(result.queryByLabelText('Next').disabled).true;
100+
expect(/** @type {HTMLLinkElement} */ (result.queryByLabelText('Next')).disabled).true;
101101
});
102102
});
103103

104104
/**
105105
*
106-
* @param {import('@testing-library/svelte').RenderResult} result
106+
* @param {import('@testing-library/svelte').RenderResult<any>} result
107107
*/
108108
function getPageItems(result) {
109109
const itemElements = result.getByRole('list').querySelectorAll('.page-item');

__tests__/StandardErrorAlert.test.js

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,27 @@ import { AlertError } from '../src/lib/common/errors';
66

77
describe('StandardErrorAlert', () => {
88
it('AlertError with string message', async () => {
9-
const result = render(StandardErrorAlert, {
10-
error: new AlertError('error message')
11-
});
9+
const result = renderStandardErrorAlert(new AlertError('error message'));
1210
expect(result.container.textContent).toContain('error message');
1311
});
1412

1513
it('AlertError with string detail', async () => {
16-
const result = render(StandardErrorAlert, {
17-
error: new AlertError({ detail: 'error message' })
18-
});
14+
const result = renderStandardErrorAlert(new AlertError({ detail: 'error message' }));
1915
expect(result.container.textContent).toContain('error message');
2016
expect(result.container.textContent).not.toContain('detail');
2117
expect(result.container.textContent).not.toContain('There has been an error');
2218
});
2319

2420
it('AlertError with array detail', async () => {
25-
const result = render(StandardErrorAlert, {
26-
error: new AlertError({ detail: ['error message'] })
27-
});
21+
const result = renderStandardErrorAlert(new AlertError({ detail: ['error message'] }));
2822
expect(result.container.textContent).toContain('error message');
2923
expect(result.container.textContent).not.toContain('detail');
3024
expect(result.container.textContent).not.toContain('There has been an error');
3125
});
3226

3327
it('AlertError with object detail and __root__ loc', async () => {
34-
const result = render(StandardErrorAlert, {
35-
error: new AlertError(
28+
const result = renderStandardErrorAlert(
29+
new AlertError(
3630
{
3731
detail: [
3832
{
@@ -44,50 +38,47 @@ describe('StandardErrorAlert', () => {
4438
},
4539
422
4640
)
47-
});
41+
);
4842
expect(result.container.textContent).toContain('error message');
4943
expect(result.container.textContent).not.toContain('detail');
5044
expect(result.container.textContent).not.toContain('There has been an error');
5145
});
5246

5347
it('AlertError with generic object message', async () => {
54-
const result = render(StandardErrorAlert, {
55-
error: new AlertError({ foo: 'bar' })
56-
});
48+
const result = renderStandardErrorAlert(new AlertError({ foo: 'bar' }));
5749
expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s);
5850
expect(result.container.textContent).toContain('There has been an error');
5951
});
6052

6153
it('AlertError with generic object message', async () => {
62-
const result = render(StandardErrorAlert, {
63-
error: new AlertError({ foo: 'bar' })
64-
});
54+
const result = renderStandardErrorAlert(new AlertError({ foo: 'bar' }));
6555
expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s);
6656
expect(result.container.textContent).toContain('There has been an error');
6757
});
6858

6959
it('Generic error with string message', async () => {
70-
const result = render(StandardErrorAlert, {
71-
error: new Error('error message')
72-
});
60+
const result = renderStandardErrorAlert(new Error('error message'));
7361
expect(result.container.textContent).toContain('error message');
7462
expect(result.container.textContent).not.toContain('There has been an error');
7563
});
7664

7765
it('Generic object message with detail', async () => {
78-
const result = render(StandardErrorAlert, {
79-
error: { detail: 'error message' }
80-
});
66+
const result = renderStandardErrorAlert({ detail: 'error message' });
8167
expect(result.container.textContent).toContain('error message');
8268
expect(result.container.textContent).not.toContain('detail');
8369
expect(result.container.textContent).not.toContain('There has been an error');
8470
});
8571

8672
it('Generic object message without detail', async () => {
87-
const result = render(StandardErrorAlert, {
88-
error: { foo: 'bar' }
89-
});
73+
const result = renderStandardErrorAlert({ foo: 'bar' });
9074
expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s);
9175
expect(result.container.textContent).toContain('There has been an error');
9276
});
9377
});
78+
79+
/**
80+
* @param {any} error
81+
*/
82+
function renderStandardErrorAlert(error) {
83+
return render(StandardErrorAlert, { error });
84+
}

__tests__/mock/jobs-list.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const data = {
3232
name: 'workflow 2'
3333
}
3434
],
35-
jobs: [
35+
jobs: /** @type {import('fractal-components/types/api').ApplyWorkflowV2[]} */ ([
3636
{
3737
id: 1,
3838
project_id: 1,
@@ -72,5 +72,5 @@ export const data = {
7272
status: 'submitted',
7373
user_email: '[email protected]'
7474
}
75-
]
75+
])
7676
};

__tests__/mock/mock-types.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @param {Partial<import('fractal-components/types/api').User & { id: number } & { group_ids_names: [number, string][]}>} fields
3+
* @returns {import('fractal-components/types/api').User & { id: number } & { group_ids_names: [number, string][]}}
4+
*/
5+
export function mockUser(fields = {}) {
6+
return {
7+
id: 1,
8+
9+
is_superuser: false,
10+
is_active: true,
11+
is_verified: true,
12+
username: null,
13+
group_ids_names: [],
14+
oauth_accounts: [],
15+
...fields
16+
};
17+
}
18+
19+
/**
20+
* @param {Partial<import('fractal-components/types/api').Group & { user_ids: number[] }>} fields
21+
* @returns {import('fractal-components/types/api').Group & { user_ids: number[] }}}
22+
*/
23+
export function mockGroup(fields = {}) {
24+
const group = /** @type {unknown} */ ({
25+
id: 1,
26+
...fields
27+
});
28+
return /** @type {import('fractal-components/types/api').Group & { user_ids: number[] }} */ (group);
29+
}
30+
31+
/**
32+
* @param {Partial<import('fractal-components/types/api').TaskV2>} fields
33+
* @returns {import('fractal-components/types/api').TaskV2}
34+
*/
35+
export function mockTask(fields = {}) {
36+
const task = /** @type {unknown} */ ({
37+
id: 1,
38+
...fields
39+
});
40+
return /** @type {import('fractal-components/types/api').TaskV2} */ (task);
41+
}
42+
43+
/**
44+
* @param {Partial<import('fractal-components/types/api').WorkflowV2>} fields
45+
* @returns {import('fractal-components/types/api').WorkflowV2}
46+
*/
47+
export function mockWorkflow(fields = {}) {
48+
const workflow = /** @type {unknown} */ ({
49+
id: 1,
50+
task_list: [],
51+
...fields
52+
});
53+
return /** @type {import('fractal-components/types/api').WorkflowV2} */ (workflow);
54+
}
55+
56+
/**
57+
* @param {Partial<import('fractal-components/types/api').WorkflowTaskV2>} fields
58+
* @returns {import('fractal-components/types/api').WorkflowTaskV2}
59+
*/
60+
export function mockWorkflowTask(fields = {}) {
61+
const workflowTask = /** @type {unknown} */ ({
62+
id: 1,
63+
task_list: [],
64+
...fields
65+
});
66+
return /** @type {import('fractal-components/types/api').WorkflowTaskV2} */ (workflowTask);
67+
}
68+
69+
/**
70+
* @param {Partial<import('fractal-components/types/api').DatasetV2>} fields
71+
* @returns {import('fractal-components/types/api').DatasetV2}
72+
*/
73+
export function mockDataset(fields = {}) {
74+
const dataset = /** @type {unknown} */ ({
75+
id: 1,
76+
...fields
77+
});
78+
return /** @type {import('fractal-components/types/api').DatasetV2} */ (dataset);
79+
}
80+
81+
/**
82+
* @param {Partial<import('fractal-components/types/api').ApplyWorkflowV2>} fields
83+
* @returns {import('fractal-components/types/api').ApplyWorkflowV2}
84+
*/
85+
export function mockApplyWorkflow(fields = {}) {
86+
const job = /** @type {unknown} */ ({
87+
id: 1,
88+
...fields
89+
});
90+
return /** @type {import('fractal-components/types/api').ApplyWorkflowV2} */ (job);
91+
}

__tests__/user_utilities.test.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import {
44
sortGroupByNameAllFirstComparator,
55
sortUserToImportSettings
66
} from '$lib/components/admin/user_utilities';
7+
import { mockGroup, mockUser } from './mock/mock-types';
78

89
it('should sort user by current superuser, then by superuser, then by email', () => {
910
let users = [
10-
{ id: 5, is_superuser: false, email: '[email protected]' },
11-
{ id: 4, is_superuser: false, email: '[email protected]' },
12-
{ id: 3, is_superuser: true, email: '[email protected]' },
13-
{ id: 2, is_superuser: true, email: '[email protected]' },
14-
{ id: 1, is_superuser: true, email: '[email protected]' }
11+
mockUser({ id: 5, is_superuser: false, email: '[email protected]' }),
12+
mockUser({ id: 4, is_superuser: false, email: '[email protected]' }),
13+
mockUser({ id: 3, is_superuser: true, email: '[email protected]' }),
14+
mockUser({ id: 2, is_superuser: true, email: '[email protected]' }),
15+
mockUser({ id: 1, is_superuser: true, email: '[email protected]' })
1516
];
1617

1718
sortUsers(users, 1);
@@ -23,9 +24,9 @@ it('should sort user by current superuser, then by superuser, then by email', ()
2324
expect(users[4].id).eq(5);
2425

2526
users = [
26-
{ id: 1, is_superuser: true, email: '[email protected]' },
27-
{ id: 2, is_superuser: true, email: '[email protected]' },
28-
{ id: 3, is_superuser: false, email: '[email protected]' }
27+
mockUser({ id: 1, is_superuser: true, email: '[email protected]' }),
28+
mockUser({ id: 2, is_superuser: true, email: '[email protected]' }),
29+
mockUser({ id: 3, is_superuser: false, email: '[email protected]' })
2930
];
3031

3132
sortUsers(users, 2);
@@ -37,9 +38,9 @@ it('should sort user by current superuser, then by superuser, then by email', ()
3738

3839
it('should sort groups by name, but keeping the All group first', () => {
3940
const groups = [
40-
{ id: 2, name: 'g2' },
41-
{ id: 1, name: 'All' },
42-
{ id: 3, name: 'g3' }
41+
mockGroup({ id: 2, name: 'g2' }),
42+
mockGroup({ id: 1, name: 'All' }),
43+
mockGroup({ id: 3, name: 'g3' })
4344
];
4445

4546
groups.sort(sortGroupByNameAllFirstComparator);
@@ -51,18 +52,18 @@ it('should sort groups by name, but keeping the All group first', () => {
5152

5253
it('should sort users to import settings', () => {
5354
const users = [
54-
{ id: 1, email: '[email protected]' }, // groups: 1
55-
{ id: 2, email: '[email protected]' }, // groups: 1, 2, 4
56-
{ id: 3, email: '[email protected]' }, // groups: 1, 3
57-
{ id: 4, email: '[email protected]' } // groups: 1, 4
55+
mockUser({ id: 1, email: '[email protected]' }), // groups: 1
56+
mockUser({ id: 2, email: '[email protected]' }), // groups: 1, 2, 4
57+
mockUser({ id: 3, email: '[email protected]' }), // groups: 1, 3
58+
mockUser({ id: 4, email: '[email protected]' }) // groups: 1, 4
5859
];
5960

6061
const desiredGroups = [2, 3];
6162
const allGroups = [
62-
{ id: 1, name: 'All', user_ids: [1, 2, 3, 4] },
63-
{ id: 2, name: 'g2', user_ids: [2] },
64-
{ id: 3, name: 'g3', user_ids: [3] },
65-
{ id: 4, name: 'g4', user_ids: [2, 4] }
63+
mockGroup({ id: 1, name: 'All', user_ids: [1, 2, 3, 4] }),
64+
mockGroup({ id: 2, name: 'g2', user_ids: [2] }),
65+
mockGroup({ id: 3, name: 'g3', user_ids: [3] }),
66+
mockGroup({ id: 4, name: 'g4', user_ids: [2, 4] })
6667
];
6768

6869
const sortedUsers = sortUserToImportSettings(users, desiredGroups, allGroups);

0 commit comments

Comments
 (0)