Skip to content

Commit 5305d02

Browse files
committed
Added tests for fetchData module
1 parent 40ae315 commit 5305d02

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

tests/utils/fetchData.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import * as core from '@actions/core';
2+
import { NotionEndpoints } from '@nishans/endpoints';
3+
import { fetchData } from '../../src/utils/fetchData';
4+
5+
afterEach(() => {
6+
jest.restoreAllMocks();
7+
});
8+
9+
it(`Should fetch data successfully`, async () => {
10+
const fetInputMock = jest
11+
.spyOn(core, 'getInput')
12+
.mockImplementationOnce(() => 'token_v2');
13+
const syncRecordValuesMock = jest
14+
.spyOn(NotionEndpoints.Queries, 'syncRecordValues')
15+
.mockImplementationOnce(async () => {
16+
return {
17+
recordMap: {
18+
block: {
19+
block_1: {
20+
role: 'comment_only',
21+
value: {
22+
id: 'block_1'
23+
}
24+
}
25+
} as any
26+
}
27+
};
28+
});
29+
30+
const data = await fetchData('block_1', 'block');
31+
32+
expect(fetInputMock).toHaveBeenCalledWith('token_v2');
33+
expect(data).toStrictEqual({
34+
id: 'block_1'
35+
});
36+
expect(syncRecordValuesMock).toHaveBeenCalledWith(
37+
{
38+
requests: [
39+
{
40+
id: 'block_1',
41+
table: 'block',
42+
version: -1
43+
}
44+
]
45+
},
46+
{
47+
token: 'token_v2',
48+
user_id: ''
49+
}
50+
);
51+
});
52+
53+
it(`Should not fetch data`, async () => {
54+
const fetInputMock = jest
55+
.spyOn(core, 'getInput')
56+
.mockImplementationOnce(() => 'token_v2');
57+
const setFailed = jest.spyOn(core, 'setFailed');
58+
const syncRecordValuesMock = jest
59+
.spyOn(NotionEndpoints.Queries, 'syncRecordValues')
60+
.mockImplementationOnce(async () => {
61+
return {
62+
recordMap: {
63+
block: {
64+
block_1: {
65+
role: 'comment_only'
66+
}
67+
} as any
68+
}
69+
};
70+
});
71+
72+
const data = await fetchData('block_1', 'block');
73+
74+
expect(fetInputMock).toHaveBeenCalledWith('token_v2');
75+
expect(data).toStrictEqual(undefined);
76+
expect(syncRecordValuesMock).toHaveBeenCalledWith(
77+
{
78+
requests: [
79+
{
80+
id: 'block_1',
81+
table: 'block',
82+
version: -1
83+
}
84+
]
85+
},
86+
{
87+
token: 'token_v2',
88+
user_id: ''
89+
}
90+
);
91+
expect(setFailed).toHaveBeenCalledWith(
92+
`Either your NOTION_TOKEN_V2 has expired or a block with id:block_1 doesn't exist`
93+
);
94+
});

0 commit comments

Comments
 (0)