diff --git a/spec/__snapshots__/snackBar.spec.ts.snap b/spec/__snapshots__/snackBar.spec.ts.snap new file mode 100644 index 000000000..4e7683846 --- /dev/null +++ b/spec/__snapshots__/snackBar.spec.ts.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`snack bar should render correctly 1`] = `
`; diff --git a/spec/snackBar.spec.ts b/spec/snackBar.spec.ts new file mode 100644 index 000000000..5f407fa0b --- /dev/null +++ b/spec/snackBar.spec.ts @@ -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); + }); +});