snack bar unit test (#594)

This commit is contained in:
VICTOR RAPHAEL BRAGA DE SALES MASCARENHAS 2019-03-19 03:21:20 -03:00 committed by Kiran Niranjan
parent e352363848
commit e3f4830c4a
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`snack bar should render correctly 1`] = `<div />`;

56
spec/snackBar.spec.ts Normal file
View File

@ -0,0 +1,56 @@
import { shallow } from 'enzyme';
import * as React from 'react';
import SnackBar from '../src/renderer/components/snack-bar';
import { ipcRenderer } from './__mocks__/electron';
describe('snack bar', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.restoreAllMocks();
});
// events label
const onEventLabel = 'on';
const removeListenerEventLabel = 'removeListener';
const windowEnterFullScreenEventLabel = 'window-enter-full-screen';
const windowLeaveFullScreenEventLabel = 'window-leave-full-screen';
it('should render correctly', () => {
const wrapper = shallow(React.createElement(SnackBar));
expect(wrapper).toMatchSnapshot();
});
it('should call mount correctly', () => {
const spy = jest.spyOn(ipcRenderer, onEventLabel);
shallow(React.createElement(SnackBar));
expect(spy).nthCalledWith(1, windowEnterFullScreenEventLabel, expect.any(Function));
expect(spy).nthCalledWith(2, windowLeaveFullScreenEventLabel, expect.any(Function));
});
it('should call unmount correctly', () => {
const spy = jest.spyOn(ipcRenderer, removeListenerEventLabel);
shallow(React.createElement(SnackBar)).unmount();
expect(spy).nthCalledWith(1, windowEnterFullScreenEventLabel, expect.any(Function));
expect(spy).nthCalledWith(2, windowLeaveFullScreenEventLabel, expect.any(Function));
});
it('should call `removeSnackBar` correctly', () => {
const spy = jest.spyOn(SnackBar.prototype, 'setState');
const expectedValue = { show: false };
shallow(React.createElement(SnackBar));
ipcRenderer.send(windowLeaveFullScreenEventLabel, null);
expect(spy).lastCalledWith(expectedValue);
jest.runOnlyPendingTimers();
});
it('should call `showSnackBar` correctly', () => {
const spy = jest.spyOn(SnackBar.prototype, 'setState');
const expectedValueFirst = { show: true };
const expectedValueSecond = { show: false };
shallow(React.createElement(SnackBar));
ipcRenderer.send(windowEnterFullScreenEventLabel, null);
expect(setTimeout).lastCalledWith(expect.any(Function), 3000);
expect(spy).nthCalledWith(1, expectedValueFirst);
jest.runOnlyPendingTimers();
expect(spy).nthCalledWith(2, expectedValueSecond);
});
});