Chore: Test coverage hunt (#36)

* chore: add brand mocking in gallery view

* feat: dev gallery app

* chore: link mock block ids to real block type api

* feat: image settings page features

* chore: more tests

* chore: keystore util and more testing

* chore: more tests

* chore: re-install lint plugin...

* chore: lint fixes

* chore: moar tests

* chore: remove brand from module.config and link gallery to edx.org brand
This commit is contained in:
Ben Warzeski
2022-03-24 11:15:32 -04:00
committed by GitHub
parent 284601d6d2
commit 09e9d865c2
30 changed files with 577 additions and 23773 deletions

View File

@@ -1,16 +1,17 @@
import { useEffect, updateState } from 'react';
import { useEffect } from 'react';
import { MockUseState } from '../testUtils';
import * as module from './hooks';
jest.mock('react', () => {
const updateStateMock = jest.fn();
return {
updateState: updateStateMock,
useState: jest.fn(val => ([{ state: val }, (newVal) => updateStateMock({ val, newVal })])),
useRef: jest.fn(val => ({ current: val })),
useEffect: jest.fn(),
useCallback: (cb, prereqs) => ({ cb, prereqs }),
};
});
jest.mock('react', () => ({
...jest.requireActual('react'),
useRef: jest.fn(val => ({ current: val })),
useEffect: jest.fn(),
useCallback: (cb, prereqs) => ({ cb, prereqs }),
}));
const state = new MockUseState(module);
describe('hooks', () => {
const locationTemp = window.location;
beforeAll(() => {
@@ -22,6 +23,9 @@ describe('hooks', () => {
afterAll(() => {
window.location = locationTemp;
});
describe('state hooks', () => {
state.testGetter(state.keys.refReady);
});
describe('initializeApp', () => {
test('calls provided function with provided data as args when useEffect is called', () => {
const mockIntialize = jest.fn(val => (val));
@@ -35,29 +39,32 @@ describe('hooks', () => {
});
});
describe('prepareEditorRef', () => {
let output;
let hook;
beforeEach(() => {
output = module.prepareEditorRef();
state.mock();
hook = module.prepareEditorRef();
});
afterEach(() => {
state.restore();
jest.clearAllMocks();
});
const key = state.keys.refReady;
test('sets refReady to false by default, ref is null', () => {
expect(output.refReady.state).toBe(false);
expect(output.editorRef.current).toBe(null);
expect(state.stateVals[key]).toEqual(false);
expect(hook.editorRef.current).toBe(null);
});
test('when useEffect triggers, refReady is set to true', () => {
expect(updateState).not.toHaveBeenCalled();
expect(state.setState[key]).not.toHaveBeenCalled();
const [cb, prereqs] = useEffect.mock.calls[0];
expect(prereqs).toStrictEqual([]);
cb();
expect(updateState).toHaveBeenCalledWith({ newVal: true, val: false });
expect(state.setState[key]).toHaveBeenCalledWith(true);
});
test('calling setEditorRef sets the ref value', () => {
const fakeEditor = { editor: 'faKe Editor' };
expect(output.editorRef.current).not.toBe(fakeEditor);
output.setEditorRef.cb(fakeEditor);
expect(output.editorRef.current).toBe(fakeEditor);
expect(hook.editorRef.current).not.toBe(fakeEditor);
hook.setEditorRef.cb(fakeEditor);
expect(hook.editorRef.current).toBe(fakeEditor);
});
});
describe('navigateTo', () => {