From e26e761d12cdd1f54bbd73fe9e770a5bed5e18ed Mon Sep 17 00:00:00 2001 From: Varsha <46579265+varshamenon4@users.noreply.github.com> Date: Fri, 14 Apr 2023 09:03:36 -0400 Subject: [PATCH] feat: add null state check (#97) --- src/api.js | 11 +++++++-- src/api.test.jsx | 60 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/api.js b/src/api.js index 0399fe28..68ec4e50 100644 --- a/src/api.js +++ b/src/api.js @@ -2,15 +2,22 @@ import { examRequiresAccessToken, store } from './data'; export function isExam() { const { exam } = store.getState().examState; - return exam.id !== null; + return !!exam?.id; } export function getExamAccess() { - const { examAccessToken } = store.getState().examState; + const { exam, examAccessToken } = store.getState().examState; + if (!exam) { + return ''; + } return examAccessToken.exam_access_token; } export async function fetchExamAccess() { + const { exam } = store.getState().examState; const { dispatch } = store; + if (!exam) { + return Promise.resolve(); + } return dispatch(examRequiresAccessToken()); } diff --git a/src/api.test.jsx b/src/api.test.jsx index dc288b5c..1cabd935 100644 --- a/src/api.test.jsx +++ b/src/api.test.jsx @@ -1,25 +1,59 @@ +import { Factory } from 'rosie'; + import { isExam, getExamAccess, fetchExamAccess } from './api'; +import { store } from './data'; describe('External API integration tests', () => { - let store; + describe('Test isExam with exam', () => { + beforeAll(() => { + jest.mock('./data'); + const mockExam = Factory.build('exam', { attempt: Factory.build('attempt') }); + const mockToken = Factory.build('examAccessToken'); + const mockState = { examState: { exam: mockExam, examAccessToken: mockToken } }; + store.getState = jest.fn().mockReturnValue(mockState); + }); - describe('Test isExam', () => { - it('Should return false if exam is not set', async () => { - expect(isExam()).toBe(false); + afterAll(() => { + jest.clearAllMocks(); + jest.resetAllMocks(); }); - }); - describe('Test getExamAccess', () => { - it('Should return empty string if no access token', async () => { - expect(getExamAccess()).toBe(''); + it('isExam should return true if exam is set', () => { + expect(isExam()).toBe(true); + }); + + it('getExamAccess should return exam access token if access token', () => { + expect(getExamAccess()).toBeTruthy(); + }); + + it('fetchExamAccess should dispatch get exam access token', () => { + const dispatchReturn = fetchExamAccess(); + expect(dispatchReturn).toBeInstanceOf(Promise); }); }); - describe('Test fetchExamAccess', () => { - it('Should dispatch get exam access token', async () => { - const mockDispatch = jest.fn(() => store.dispatch); - const mockState = jest.fn(() => store.getState); - const dispatchReturn = fetchExamAccess(mockDispatch, mockState); + describe('Test isExam without exam', () => { + beforeAll(() => { + jest.mock('./data'); + const mockState = { examState: { exam: null, examAccessToken: null } }; + store.getState = jest.fn().mockReturnValue(mockState); + }); + + afterAll(() => { + jest.clearAllMocks(); + jest.resetAllMocks(); + }); + + it('isExam should return false if exam is not set', () => { + expect(isExam()).toBe(false); + }); + + it('getExamAccess should return empty string if exam access token not set', () => { + expect(getExamAccess()).toBeFalsy(); + }); + + it('fetchExamAccess should not dispatch get exam access token', () => { + const dispatchReturn = fetchExamAccess(); expect(dispatchReturn).toBeInstanceOf(Promise); }); });