Skip to content

Commit 9457a1e

Browse files
committed
chore: fix unit test
1 parent cd3f8cb commit 9457a1e

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
run: npx nx affected -t lint --parallel=7 --exclude='*,!tag:type:pkg'
4545

4646
- name: Run Affected Test
47-
run: npx nx affected -t test --parallel=3 --exclude='*,!tag:type:pkg'
47+
run: npx nx affected -t test --parallel=3 --exclude='*,!tag:type:pkg' --skip-cache
4848

4949
# - name: E2E Test for Next.js Dev
5050
# run: |

packages/sdk/__tests__/dom.spec.ts

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('createScript', () => {
88
it('should create a new script element if one does not exist', () => {
99
const url = 'https://example.com/script.js';
1010
const cb = jest.fn();
11-
const { script, needAttach } = createScript(url, cb);
11+
const { script, needAttach } = createScript({ url, cb });
1212

1313
expect(script.tagName).toBe('SCRIPT');
1414
expect(script.src).toBe(url);
@@ -19,7 +19,7 @@ describe('createScript', () => {
1919
const url = 'https://example.com/script.js';
2020
const cb = jest.fn();
2121
document.body.innerHTML = `<script src="${url}"></script>`;
22-
const { script, needAttach } = createScript(url, cb);
22+
const { script, needAttach } = createScript({ url, cb });
2323

2424
expect(script.tagName).toBe('SCRIPT');
2525
expect(script.src).toBe(url);
@@ -30,7 +30,7 @@ describe('createScript', () => {
3030
const url = 'https://example.com/script.js';
3131
const cb = jest.fn();
3232
const attrs = { async: true, 'data-test': 'test' };
33-
const { script } = createScript(url, cb, attrs);
33+
const { script } = createScript({ url, cb, attrs });
3434

3535
expect(script.async).toBe(true);
3636
expect(script.getAttribute('data-test')).toBe('test');
@@ -39,7 +39,7 @@ describe('createScript', () => {
3939
it('should call the callback when the script loads', () => {
4040
const url = 'https://example.com/script.js';
4141
const cb = jest.fn();
42-
const { script, needAttach } = createScript(url, cb);
42+
const { script, needAttach } = createScript({ url, cb });
4343

4444
if (needAttach) {
4545
document.body.appendChild(script);
@@ -52,7 +52,12 @@ describe('createScript', () => {
5252
it('should call the callback when the script times out', () => {
5353
const url = 'https://example.com/script.js';
5454
const cb = jest.fn();
55-
createScript(url, cb, {}, () => ({ timeout: 100 }));
55+
createScript({
56+
url,
57+
cb,
58+
attrs: {},
59+
createScriptHook: () => ({ timeout: 100 }),
60+
});
5661

5762
setTimeout(() => {
5863
expect(cb).toHaveBeenCalled();
@@ -71,7 +76,7 @@ describe('createScript', () => {
7176
it('should use the default timeout of 20000ms if no timeout is specified', () => {
7277
const url = 'https://example.com/script.js';
7378
const cb = jest.fn();
74-
const { script } = createScript(url, cb);
79+
const { script } = createScript({ url, cb });
7580

7681
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 20000);
7782
});
@@ -80,7 +85,12 @@ describe('createScript', () => {
8085
const url = 'https://example.com/script.js';
8186
const cb = jest.fn();
8287
const customTimeout = 5000;
83-
createScript(url, cb, {}, () => ({ timeout: customTimeout }));
88+
createScript({
89+
url,
90+
cb,
91+
attrs: {},
92+
createScriptHook: () => ({ timeout: customTimeout }),
93+
});
8494

8595
expect(setTimeout).toHaveBeenCalledWith(
8696
expect.any(Function),
@@ -91,7 +101,7 @@ describe('createScript', () => {
91101
it('should clear the timeout when the script loads successfully', () => {
92102
const url = 'https://example.com/script.js';
93103
const cb = jest.fn();
94-
const { script, needAttach } = createScript(url, cb);
104+
const { script, needAttach } = createScript({ url, cb });
95105

96106
if (needAttach) {
97107
document.body.appendChild(script);
@@ -104,7 +114,7 @@ describe('createScript', () => {
104114
it('should clear the timeout when the script fails to load', () => {
105115
const url = 'https://example.com/script.js';
106116
const cb = jest.fn();
107-
const { script, needAttach } = createScript(url, cb);
117+
const { script, needAttach } = createScript({ url, cb });
108118

109119
if (needAttach) {
110120
document.body.appendChild(script);
@@ -124,7 +134,11 @@ describe('createLink', () => {
124134
it('should create a new link element if one does not exist', () => {
125135
const url = 'https://example.com/script.js';
126136
const cb = jest.fn();
127-
const { link, needAttach } = createLink(url, cb, { as: 'script' });
137+
const { link, needAttach } = createLink({
138+
url,
139+
cb,
140+
attrs: { as: 'script' },
141+
});
128142

129143
expect(link.tagName).toBe('LINK');
130144
expect(link.href).toBe(url);
@@ -136,9 +150,13 @@ describe('createLink', () => {
136150
const url = 'https://example.com/script.js';
137151
const cb = jest.fn();
138152
document.head.innerHTML = `<link href="${url}" rel="preload" as="script">`;
139-
const { link, needAttach } = createLink(url, cb, {
140-
rel: 'preload',
141-
as: 'script',
153+
const { link, needAttach } = createLink({
154+
url,
155+
cb,
156+
attrs: {
157+
rel: 'preload',
158+
as: 'script',
159+
},
142160
});
143161

144162
expect(link.tagName).toBe('LINK');
@@ -150,7 +168,7 @@ describe('createLink', () => {
150168
const url = 'https://example.com/script.js';
151169
const cb = jest.fn();
152170
const attrs = { rel: 'preload', as: 'script', 'data-test': 'test' };
153-
const { link } = createLink(url, cb, attrs);
171+
const { link } = createLink({ url, cb, attrs });
154172

155173
expect(link.rel).toBe('preload');
156174
expect(link.getAttribute('as')).toBe('script');
@@ -160,7 +178,11 @@ describe('createLink', () => {
160178
it('should call the callback when the link loads', () => {
161179
const url = 'https://example.com/script.js';
162180
const cb = jest.fn();
163-
const { link, needAttach } = createLink(url, cb, { as: 'script' });
181+
const { link, needAttach } = createLink({
182+
url,
183+
cb,
184+
attrs: { as: 'script' },
185+
});
164186

165187
if (needAttach) {
166188
document.head.appendChild(link);
@@ -173,7 +195,11 @@ describe('createLink', () => {
173195
it('should call the callback when the link fails to load', () => {
174196
const url = 'https://example.com/script.js';
175197
const cb = jest.fn();
176-
const { link, needAttach } = createLink(url, cb, { as: 'script' });
198+
const { link, needAttach } = createLink({
199+
url,
200+
cb,
201+
attrs: { as: 'script' },
202+
});
177203

178204
if (needAttach) {
179205
document.head.appendChild(link);
@@ -190,7 +216,12 @@ describe('createLink', () => {
190216
customLink.href = url;
191217
customLink.rel = 'preload';
192218
customLink.setAttribute('as', 'script');
193-
const { link } = createLink(url, cb, {}, () => customLink);
219+
const { link } = createLink({
220+
url,
221+
cb,
222+
attrs: {},
223+
createLinkHook: () => customLink,
224+
});
194225

195226
expect(link).toBe(customLink);
196227
});

0 commit comments

Comments
 (0)