-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe2512e
commit 8c0aa26
Showing
6 changed files
with
252 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
|
||
import { ref } from 'vue' | ||
import { baseUrl, waitUntilValueChanges } from '../mocks/utils' | ||
import { useList } from './index' | ||
|
||
describe('useList', () => { | ||
it('it returns expected object', async () => { | ||
interface User { | ||
name: string | ||
email: string | ||
} | ||
|
||
let users = useList<User>({ | ||
baseUrl, | ||
doctype: 'User', | ||
fields: ['name', 'email'], | ||
groupBy: 'name', | ||
orderBy: 'email asc', | ||
start: 0, | ||
limit: 2, | ||
immediate: false, | ||
}) | ||
|
||
// Verify initial state | ||
expect(users.data).toBe(null) | ||
expect(users.error).toBe(null) | ||
expect(users.hasNextPage).toBe(false) | ||
expect(typeof users.fetch).toBe('function') | ||
|
||
// fetch | ||
await users.fetch() | ||
|
||
// Verify final state | ||
expect(users.data).toStrictEqual([ | ||
{ name: 'User1', email: '[email protected]' }, | ||
{ name: 'User2', email: '[email protected]' }, | ||
]) | ||
expect(users.error).toBe(null) | ||
expect(users.isFinished).toBe(true) | ||
expect(users.loading).toBe(false) | ||
}) | ||
|
||
it('handles pagination correctly', async () => { | ||
const users = useList({ | ||
baseUrl, | ||
doctype: 'User', | ||
fields: ['name', 'email'], | ||
limit: 2, | ||
immediate: false, | ||
}) | ||
|
||
await users.fetch() | ||
|
||
expect(users.hasNextPage).toBe(true) | ||
expect(users.hasPreviousPage).toBe(false) | ||
expect(users.start).toBe(0) | ||
|
||
users.next() | ||
await waitUntilValueChanges(() => users.data) | ||
|
||
expect(users.start).toBe(2) | ||
expect(users.hasPreviousPage).toBe(true) | ||
expect(users.data).toStrictEqual([ | ||
{ name: 'User3', email: '[email protected]' }, | ||
{ name: 'User4', email: '[email protected]' }, | ||
]) | ||
|
||
await users.previous() | ||
expect(users.start).toBe(0) | ||
expect(users.hasPreviousPage).toBe(false) | ||
}) | ||
|
||
it('dynamic filters should refetch the list', async () => { | ||
const query = ref('user1') | ||
const users = useList({ | ||
baseUrl, | ||
doctype: 'User', | ||
fields: ['name', 'email'], | ||
filters: { | ||
email: ['like', query], | ||
}, | ||
limit: 3, | ||
}) | ||
|
||
await waitUntilValueChanges(() => users.data) | ||
expect(users.data).toStrictEqual([ | ||
{ name: 'User1', email: '[email protected]' }, | ||
]) | ||
|
||
query.value = 'user2' | ||
await waitUntilValueChanges(() => users.data) | ||
expect(users.data).toStrictEqual([ | ||
{ name: 'User2', email: '[email protected]' }, | ||
]) | ||
}) | ||
|
||
it('params are parsed and sent to server correctly', async () => { | ||
const query = ref('user1') | ||
const users = useList({ | ||
baseUrl, | ||
doctype: 'User', | ||
fields: ['name', 'email'], | ||
filters: { | ||
name: 'User1', | ||
email: ['like', query], | ||
}, | ||
limit: 2, | ||
immediate: false, | ||
}) | ||
|
||
// intercept fetch and check request params | ||
const fetchSpy = vi.spyOn(global, 'fetch') | ||
|
||
await users.fetch() | ||
|
||
let searchParams = new URLSearchParams({ | ||
fields: JSON.stringify(['name', 'email']), | ||
filters: JSON.stringify({ | ||
name: 'User1', | ||
email: ['like', '%user1%'], | ||
}), | ||
start: '0', | ||
limit: '2', | ||
}) | ||
|
||
expect(fetchSpy).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
`${baseUrl}/api/v2/document/User?${searchParams.toString()}`, | ||
), | ||
expect.any(Object), | ||
) | ||
|
||
fetchSpy.mockRestore() | ||
}) | ||
|
||
it('transforms data using transform function', async () => { | ||
const users = useList({ | ||
baseUrl, | ||
doctype: 'User', | ||
fields: ['name', 'email'], | ||
transform: (data) => { | ||
return data.map((user) => ({ | ||
...user, | ||
displayName: user.name.toUpperCase(), | ||
})) | ||
}, | ||
immediate: false, | ||
}) | ||
|
||
await users.fetch() | ||
// @ts-ignore | ||
expect(users.data[0].displayName).toBe('USER1') | ||
}) | ||
|
||
it('handles errors correctly', async () => { | ||
let errorCaught = null | ||
const users = useList({ | ||
baseUrl, | ||
doctype: 'InvalidDoctype', | ||
onError: (error) => { | ||
errorCaught = error | ||
}, | ||
immediate: false, | ||
}) | ||
|
||
await users.fetch() | ||
expect(users.error).toBeTruthy() | ||
expect(errorCaught).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,21 @@ | ||
export let url = (path: string) => | ||
new URL(path, 'http://example.com').toString() | ||
import { watch } from 'vue' | ||
|
||
export let baseUrl = 'http://example.com' | ||
|
||
export let url = (path: string) => new URL(path, baseUrl).toString() | ||
|
||
export function waitUntilValueChanges( | ||
getter: () => any, | ||
timeout = 1000, | ||
): Promise<void> { | ||
return new Promise((resolve) => { | ||
let stop = watch(getter, () => { | ||
stop() | ||
resolve() | ||
}) | ||
setTimeout(() => { | ||
stop() | ||
resolve() | ||
}, timeout) | ||
}) | ||
} |