Skip to content

Commit e162a3e

Browse files
committed
[remove duplicates] move abortRequest to sdk
1 parent bece4e0 commit e162a3e

7 files changed

+402
-16
lines changed

jest.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const config: Config = {
66
maxWorkers: 1, // Fixed https://github.com/jestjs/jest/issues/11617#issuecomment-1028651059
77
rootDir: './src',
88
preset: 'ts-jest',
9-
resetMocks: true,
9+
resetMocks: false,
1010
testEnvironment: 'node',
1111
testMatch: [ '**/*.spec.ts' ],
1212
collectCoverageFrom: [ 'src/**/*.ts' ],

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export * from './utils/enums'
66
export { createContract } from './contracts'
77
export { default as StakeWiseSDK } from './StakeWiseSDK'
88
export { default as localStorage } from './modules/local-storage'
9-
export { wrapAbortPromise, AbortPromise } from './modules/gql-module'
9+
export { wrapAbortPromise, AbortPromise, AbortRequest } from './modules/gql-module'
1010

1111
export {
1212
configs,

src/modules/gql-module/abortCallback.ts

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class AbortCallback {
1414

1515
then(onSuccess: (data: any) => any, onError?: (error: any) => any) {
1616
if (this.isAborted) {
17-
const dummyPromise = new Promise(() => {})
18-
1917
return new AbortCallback(dummyPromise, this.onAbort)
2018
}
2119

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import AbortPromise from './abortPromise'
2+
import AbortCallback from './abortCallback'
3+
4+
5+
describe('AbortPromise', () => {
6+
7+
it('resolves the promise', async () => {
8+
const mock = 'response data'
9+
const abortPromise = new AbortPromise((resolve) => {
10+
resolve(mock)
11+
})
12+
13+
const result = await abortPromise
14+
15+
expect(result).toEqual(mock)
16+
})
17+
18+
it('rejects the promise', async () => {
19+
const mock = 'error'
20+
const abortPromise = new AbortPromise((resolve, reject) => {
21+
reject(mock)
22+
})
23+
24+
await expect(abortPromise).rejects.toEqual(mock)
25+
})
26+
27+
it('calls then on promise resolve', async () => {
28+
const mockThenFn = jest.fn()
29+
const mockCatchFn = jest.fn()
30+
31+
const abortPromise = new AbortPromise((resolve, reject) => {
32+
resolve(null)
33+
})
34+
.then(mockThenFn, mockCatchFn)
35+
36+
await abortPromise
37+
38+
expect(mockThenFn).toBeCalledTimes(1)
39+
expect(mockCatchFn).toBeCalledTimes(0)
40+
})
41+
42+
it('calls catch on promise reject', async () => {
43+
const mockThenFn = jest.fn()
44+
const mockCatchFn = jest.fn()
45+
46+
const abortPromise = new AbortPromise((resolve, reject) => {
47+
reject(null)
48+
})
49+
.then(mockThenFn, mockCatchFn)
50+
51+
await abortPromise
52+
53+
expect(mockThenFn).toBeCalledTimes(0)
54+
expect(mockCatchFn).toBeCalledTimes(1)
55+
})
56+
57+
it('doesn\'t resolve aborted promise', async () => {
58+
const mockThenFn = jest.fn()
59+
const mockCatchFn = jest.fn()
60+
61+
const abortPromise = new AbortPromise((resolve, reject) => {
62+
resolve(null)
63+
})
64+
.then(mockThenFn, mockCatchFn)
65+
66+
abortPromise.abort()
67+
68+
expect(mockThenFn).toBeCalledTimes(0)
69+
expect(mockCatchFn).toBeCalledTimes(0)
70+
})
71+
72+
it('doesn\'t reject aborted promise', async () => {
73+
const mockThenFn = jest.fn()
74+
const mockCatchFn = jest.fn()
75+
76+
const abortPromise = new AbortPromise((resolve, reject) => {
77+
reject(null)
78+
})
79+
.then(mockThenFn, mockCatchFn)
80+
81+
abortPromise.abort()
82+
83+
expect(mockThenFn).toBeCalledTimes(0)
84+
expect(mockCatchFn).toBeCalledTimes(0)
85+
})
86+
87+
it('resolves multiple promises in "all" method', async () => {
88+
const mock1 = 'response data 1'
89+
const mock2 = 'response data 2'
90+
91+
const promise1 = new Promise((resolve) => resolve(mock1))
92+
const promise2 = new Promise((resolve) => resolve(mock2))
93+
94+
const result = await AbortPromise.all([ promise1, promise2 ])
95+
96+
expect(result).toEqual([ mock1, mock2 ])
97+
})
98+
99+
it('resolves promise in "race" method', async () => {
100+
const mock = 'response data'
101+
102+
const promise1 = new Promise((resolve) => resolve(mock))
103+
const promise2 = new Promise(() => {})
104+
105+
const result = await AbortPromise.race([ promise1, promise2 ])
106+
107+
expect(result).toEqual(mock)
108+
})
109+
110+
it('resolves multiple abort promises in "all" method', async () => {
111+
const mock1 = 'response data 1'
112+
const mock2 = 'response data 2'
113+
114+
const promise1 = new AbortPromise((resolve) => resolve(mock1))
115+
const promise2 = new AbortPromise((resolve) => resolve(mock2))
116+
117+
const result = await AbortPromise.all([ promise1, promise2 ])
118+
119+
expect(result).toEqual([ mock1, mock2 ])
120+
})
121+
122+
it('doesn\'t resolve multiple promises in "all" method on abort', async () => {
123+
const abort1 = jest.fn()
124+
const abort2 = jest.fn()
125+
const mockThenFn = jest.fn()
126+
const mockCatchFn = jest.fn()
127+
128+
const dummyPromise = new Promise(() => {})
129+
130+
const promise1 = new AbortCallback(dummyPromise, abort1)
131+
const promise2 = new AbortCallback(dummyPromise, abort2)
132+
133+
// @ts-ignore
134+
const promise = AbortPromise.all([ promise1, promise2 ])
135+
.then(mockThenFn, mockCatchFn)
136+
137+
promise.abort()
138+
139+
expect(abort1).toBeCalledTimes(1)
140+
expect(abort2).toBeCalledTimes(1)
141+
expect(mockThenFn).toBeCalledTimes(0)
142+
expect(mockCatchFn).toBeCalledTimes(0)
143+
})
144+
145+
it('doesn\'t reject multiple promises in "all" method on abort', async () => {
146+
const mockThenFn = jest.fn()
147+
const mockCatchFn = jest.fn()
148+
149+
const promise = AbortPromise.all([ Promise.reject(), Promise.reject() ])
150+
.then(mockThenFn, mockCatchFn)
151+
152+
promise.abort()
153+
154+
expect(mockThenFn).toBeCalledTimes(0)
155+
expect(mockCatchFn).toBeCalledTimes(0)
156+
})
157+
})

0 commit comments

Comments
 (0)