Skip to content

Commit 3fab5c7

Browse files
committed
cleanup
1 parent fdcd949 commit 3fab5c7

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

tests/api.test.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { beforeEach, afterEach, describe, test, expect } from 'vitest'
22
import { api, PlexusApi } from '@plexusjs/api'
33
// only imported here to make sure it works in testing environment (node), not needed by user
44
import 'isomorphic-fetch'
5+
import { PlexusError } from '@plexusjs/utils'
56

67
// if(globalThis.fetch === undefined) globalThis.fetch = fetch as any as (input: RequestInfo, init?: RequestInit) => Promise<Response>;
78

89
let myApi: PlexusApi
910

1011
beforeEach(() => {
11-
myApi = api()
12+
myApi = api('', {
13+
abortOnTimeout: true,
14+
})
1215
})
1316
describe('Testing Api Function', () => {
1417
test('Send a get request to google', async () => {
@@ -87,7 +90,55 @@ describe('Testing Api Function', () => {
8790
apiUsingOnResponse.post('https://google.com/this/url/doesnt/exist')
8891
).rejects.toThrow()
8992
})
90-
})
93+
94+
test('can set a timeout with abort', async () => {
95+
// const value = state(1)
96+
const apiUsingOnResponse = api('', {
97+
timeout: 1000,
98+
throws: true,
99+
abortOnTimeout: true,
100+
})
101+
102+
apiUsingOnResponse.options({
103+
headers: {
104+
custom: 'header',
105+
},
106+
})
107+
// console.log(myApi.config)
108+
expect(apiUsingOnResponse.config).toBeDefined()
109+
expect(apiUsingOnResponse.config.headers).toBeDefined()
110+
expect(apiUsingOnResponse.config.headers['custom']).toBe('header')
111+
112+
let errorOccurred = false
113+
114+
try {
115+
await apiUsingOnResponse.post('http://httpstat.us/526?sleep=2800')
116+
} catch (error) {
117+
console.log(error)
118+
errorOccurred = true
119+
}
120+
expect(errorOccurred).toBe(true)
121+
122+
// Wait for the sleep duration of the request endpoint
123+
await new Promise((resolve) => setTimeout(resolve, 3000))
124+
125+
// Check if a second error is thrown
126+
errorOccurred = false
127+
128+
try {
129+
await apiUsingOnResponse.post('http://httpstat.us/526?sleep=2800')
130+
} catch (error) {
131+
console.log(error)
132+
if (error instanceof PlexusError) {
133+
// if it's a PlexusError, it means this is the timeout error
134+
return
135+
}
136+
errorOccurred = true
137+
}
138+
139+
expect(errorOccurred).toBe(false)
140+
})
141+
}, 10000)
91142
describe("Test the API's baseURL capabilities", () => {
92143
const myApi2 = api('https://google.com').setHeaders({
93144
'Content-Type': 'application/json',

tests/edgecases.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('Collection Relations', () => {
8989
)
9090

9191
test('Batching race condition with selectors', () => {
92-
batch(() => { })
92+
batch(() => {})
9393
})
9494
})
9595

tests/efficiency.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const users1kRelated = Array.from(
3131
id: randUuid(),
3232
firstName: randFirstName(),
3333
appointmentId: randUuid(),
34-
} as UserType)
34+
}) as UserType
3535
)
3636

3737
const appointments1kRelated = users1kRelated.map(
@@ -41,7 +41,7 @@ const appointments1kRelated = users1kRelated.map(
4141
userId: user.id,
4242
date: randFutureDate().getTime(),
4343
name: randBook().title,
44-
} as AppointmentType)
44+
}) as AppointmentType
4545
)
4646

4747
// check the .cache directory for the generated data. If it doesn't exist, it will be generated. Need users1k.json, users10k.json, users10kRelated.json, appointments10kRelated.json

tests/react.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('Test react integration (useDeposit)', () => {
186186
const { value, save, edit } = useDeposit(
187187
{ ...def },
188188
{
189-
onEdit(k, v) { },
189+
onEdit(k, v) {},
190190
onSave(payload) {
191191
setVal(payload.name ?? '')
192192
},

tests/timed.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ describe('Ephemeral Collection data', () => {
3737
expect(decayingUsers.value.length).toBe(1)
3838
}, DEFAULT_DECAY_RATE + 10)
3939
// and then it should decay past the decay rate
40-
setTimeout(() => {
41-
expect(decayingUsers.value.length).toBe(0)
42-
console.log('done! Thing is decayed!')
43-
resolve(true)
44-
}, DEFAULT_DECAY_RATE * 2 + 10)
40+
setTimeout(
41+
() => {
42+
expect(decayingUsers.value.length).toBe(0)
43+
console.log('done! Thing is decayed!')
44+
resolve(true)
45+
},
46+
DEFAULT_DECAY_RATE * 2 + 10
47+
)
4548
}),
4649
{
4750
timeout: DEFAULT_DECAY_RATE * 2 + 100,

tests/utils.tests.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ describe('Test helper functions from the utils module', () => {
7777
expect(merged4).toEqual({ a: 2, b: { c: [{ d: 2, e: [4, 5, 6] }] } })
7878
})
7979
test('Test isAsyncFunction', () => {
80-
expect(isAsyncFunction(async () => { })).toBe(true)
81-
expect(isAsyncFunction(() => { })).toBe(false)
80+
expect(isAsyncFunction(async () => {})).toBe(true)
81+
expect(isAsyncFunction(() => {})).toBe(false)
8282
})
8383
test('Test convertStringToThing', () => {
8484
expect(convertStringToThing('{"a":1}')).toEqual({ a: 1 })

0 commit comments

Comments
 (0)