From 192b67c5078ab697683bbed3c426e17816f58ed1 Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Fri, 6 Sep 2024 10:35:28 -0700 Subject: [PATCH] test: improve test coverage of library-authoring/data/api.ts --- src/library-authoring/data/api.mocks.ts | 1 + src/library-authoring/data/api.test.ts | 96 ++++++++++++------------- src/testUtils.tsx | 1 + 3 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/library-authoring/data/api.mocks.ts b/src/library-authoring/data/api.mocks.ts index f7f0fe06d7..863d25607c 100644 --- a/src/library-authoring/data/api.mocks.ts +++ b/src/library-authoring/data/api.mocks.ts @@ -1,3 +1,4 @@ +/* istanbul ignore file */ import { createAxiosError } from '../../testUtils'; import * as api from './api'; diff --git a/src/library-authoring/data/api.test.ts b/src/library-authoring/data/api.test.ts index 557488900d..428b0b3aa1 100644 --- a/src/library-authoring/data/api.test.ts +++ b/src/library-authoring/data/api.test.ts @@ -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); + }); }); }); diff --git a/src/testUtils.tsx b/src/testUtils.tsx index d2895eaab4..0de3acd3e5 100644 --- a/src/testUtils.tsx +++ b/src/testUtils.tsx @@ -1,3 +1,4 @@ +/* istanbul ignore file */ /* eslint-disable react/require-default-props */ /* eslint-disable react/prop-types */ /* eslint-disable import/no-extraneous-dependencies */