Skip to content

Commit

Permalink
feat: add null state check (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
varshamenon4 authored Apr 14, 2023
1 parent 5161dd3 commit e26e761
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
60 changes: 47 additions & 13 deletions src/api.test.jsx
Original file line number Diff line number Diff line change
@@ -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);
});
});
Expand Down

0 comments on commit e26e761

Please sign in to comment.