forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
66 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,71 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useTaxonomyListData } from './api'; | ||
import { useQuery, useMutation } from '@tanstack/react-query'; | ||
import { useTaxonomyListData, useExportTaxonomy } from './api'; | ||
import { downloadDataAsFile } from '../../../utils'; | ||
|
||
const mockHttpClient = { | ||
get: jest.fn(), | ||
}; | ||
|
||
jest.mock('@tanstack/react-query', () => ({ | ||
useQuery: jest.fn(), | ||
useMutation: jest.fn(), | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/auth', () => ({ | ||
getAuthenticatedHttpClient: jest.fn(), | ||
getAuthenticatedHttpClient: jest.fn(() => mockHttpClient), | ||
})); | ||
|
||
jest.mock('../../../utils', () => ({ | ||
downloadDataAsFile: jest.fn(), | ||
})); | ||
|
||
describe('taxonomy API: useTaxonomyListData', () => { | ||
describe('taxonomy API', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call useQuery with the correct parameters', () => { | ||
it('useTaxonomyListData should call useQuery with the correct parameters', () => { | ||
useTaxonomyListData(); | ||
|
||
expect(useQuery).toHaveBeenCalledWith({ | ||
queryKey: ['taxonomyList'], | ||
queryFn: expect.any(Function), | ||
}); | ||
}); | ||
|
||
it('useExportTaxonomy should export data correctly', async () => { | ||
useMutation.mockImplementation((exportFunc) => exportFunc); | ||
|
||
const mockResponseJson = { | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
data: { tags: 'tags' }, | ||
}; | ||
const mockResponseCsv = { | ||
headers: { | ||
'content-type': 'text', | ||
}, | ||
data: 'This is a CSV', | ||
}; | ||
|
||
const exportTaxonomy = useExportTaxonomy(); | ||
|
||
mockHttpClient.get.mockResolvedValue(mockResponseJson); | ||
await exportTaxonomy({ pk: 1, format: 'json', name: 'testFile' }); | ||
|
||
expect(downloadDataAsFile).toHaveBeenCalledWith( | ||
JSON.stringify(mockResponseJson.data, null, 2), | ||
'application/json', | ||
'testFile.json', | ||
); | ||
|
||
mockHttpClient.get.mockResolvedValue(mockResponseCsv); | ||
await exportTaxonomy({ pk: 1, format: 'csv', name: 'testFile' }); | ||
expect(downloadDataAsFile).toHaveBeenCalledWith( | ||
mockResponseCsv.data, | ||
'text', | ||
'testFile.csv', | ||
); | ||
}); | ||
}); |
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