-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve test coverage of library-authoring/data/api.ts
- Loading branch information
1 parent
227864c
commit 192b67c
Showing
3 changed files
with
49 additions
and
49 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,3 +1,4 @@ | ||
/* istanbul ignore file */ | ||
import { createAxiosError } from '../../testUtils'; | ||
import * as api from './api'; | ||
|
||
|
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,65 +1,63 @@ | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { initializeMockApp } from '@edx/frontend-platform'; | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
import { | ||
commitLibraryChanges, | ||
createLibraryBlock, | ||
getCommitLibraryChangesUrl, | ||
getCreateLibraryBlockUrl, | ||
revertLibraryChanges, | ||
} from './api'; | ||
|
||
let axiosMock; | ||
|
||
describe('library api calls', () => { | ||
beforeEach(() => { | ||
initializeMockApp({ | ||
authenticatedUser: { | ||
userId: 3, | ||
username: 'abc123', | ||
administrator: true, | ||
roles: [], | ||
}, | ||
import { initializeMocks } from '../../testUtils'; | ||
import * as api from './api'; | ||
|
||
describe('library data API', () => { | ||
describe('getLibraryBlockTypes', () => { | ||
it('throws an error for invalid libraryId', () => { | ||
const err = 'The current API for block types requires a libraryId.'; | ||
expect(() => api.getLibraryBlockTypes('')).rejects.toThrow(err); | ||
// @ts-ignore | ||
expect(() => api.getLibraryBlockTypes()).rejects.toThrow(err); | ||
}); | ||
|
||
axiosMock = new MockAdapter(getAuthenticatedHttpClient()); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
axiosMock.restore(); | ||
describe('getContentLibrary', () => { | ||
it('throws an error for invalid libraryId', () => { | ||
expect(api.getContentLibrary('')).rejects.toThrow('libraryId is required'); | ||
// @ts-ignore | ||
expect(() => api.getContentLibrary()).rejects.toThrow('libraryId is required'); | ||
}); | ||
}); | ||
|
||
it('should create library block', async () => { | ||
const libraryId = 'lib:org:1'; | ||
const url = getCreateLibraryBlockUrl(libraryId); | ||
axiosMock.onPost(url).reply(200); | ||
await createLibraryBlock({ | ||
libraryId, | ||
blockType: 'html', | ||
definitionId: '1', | ||
describe('createLibraryBlock', () => { | ||
it('should create library block', async () => { | ||
const { axiosMock } = initializeMocks(); | ||
const libraryId = 'lib:org:1'; | ||
const url = api.getCreateLibraryBlockUrl(libraryId); | ||
axiosMock.onPost(url).reply(200); | ||
await api.createLibraryBlock({ | ||
libraryId, | ||
blockType: 'html', | ||
definitionId: '1', | ||
}); | ||
|
||
expect(axiosMock.history.post[0].url).toEqual(url); | ||
}); | ||
|
||
expect(axiosMock.history.post[0].url).toEqual(url); | ||
}); | ||
|
||
it('should commit library changes', async () => { | ||
const libraryId = 'lib:org:1'; | ||
const url = getCommitLibraryChangesUrl(libraryId); | ||
axiosMock.onPost(url).reply(200); | ||
describe('commitLibraryChanges', () => { | ||
it('should commit library changes', async () => { | ||
const { axiosMock } = initializeMocks(); | ||
const libraryId = 'lib:org:1'; | ||
const url = api.getCommitLibraryChangesUrl(libraryId); | ||
axiosMock.onPost(url).reply(200); | ||
|
||
await commitLibraryChanges(libraryId); | ||
await api.commitLibraryChanges(libraryId); | ||
|
||
expect(axiosMock.history.post[0].url).toEqual(url); | ||
expect(axiosMock.history.post[0].url).toEqual(url); | ||
}); | ||
}); | ||
|
||
it('should revert library changes', async () => { | ||
const libraryId = 'lib:org:1'; | ||
const url = getCommitLibraryChangesUrl(libraryId); | ||
axiosMock.onDelete(url).reply(200); | ||
describe('revertLibraryChanges', () => { | ||
it('should revert library changes', async () => { | ||
const { axiosMock } = initializeMocks(); | ||
const libraryId = 'lib:org:1'; | ||
const url = api.getCommitLibraryChangesUrl(libraryId); | ||
axiosMock.onDelete(url).reply(200); | ||
|
||
await revertLibraryChanges(libraryId); | ||
await api.revertLibraryChanges(libraryId); | ||
|
||
expect(axiosMock.history.delete[0].url).toEqual(url); | ||
expect(axiosMock.history.delete[0].url).toEqual(url); | ||
}); | ||
}); | ||
}); |
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