Skip to content

Commit

Permalink
test: improve test coverage of library-authoring/data/api.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Sep 6, 2024
1 parent 227864c commit 192b67c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
1 change: 1 addition & 0 deletions src/library-authoring/data/api.mocks.ts
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';

Expand Down
96 changes: 47 additions & 49 deletions src/library-authoring/data/api.test.ts
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);
});
});
});
1 change: 1 addition & 0 deletions src/testUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
/* eslint-disable react/require-default-props */
/* eslint-disable react/prop-types */
/* eslint-disable import/no-extraneous-dependencies */
Expand Down

0 comments on commit 192b67c

Please sign in to comment.