From 85b2e730fbccc0563dff0b2e5f27834e58db567b Mon Sep 17 00:00:00 2001 From: "Victor R. Braga de Sales Mascarenhasm" Date: Wed, 13 Mar 2019 18:38:27 -0300 Subject: [PATCH] snack bar unit test --- spec/__snapshots__/aboutApp.spec.ts.snap | 4 +- spec/__snapshots__/basicAuth.spec.ts.snap | 4 +- .../downloadManager.spec.ts.snap | 4 +- spec/__snapshots__/loadingScreen.spec.ts.snap | 4 +- spec/__snapshots__/screenPicker.spec.ts.snap | 4 +- .../screenSharingIndicator.spec.ts.snap | 4 +- spec/__snapshots__/snackBar.spec.ts.snap | 61 +++++++++++++++++++ spec/snackBar.spec.ts | 56 +++++++++++++++++ 8 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 spec/__snapshots__/snackBar.spec.ts.snap create mode 100644 spec/snackBar.spec.ts diff --git a/spec/__snapshots__/aboutApp.spec.ts.snap b/spec/__snapshots__/aboutApp.spec.ts.snap index 1c4df27d5..d9d25494f 100644 --- a/spec/__snapshots__/aboutApp.spec.ts.snap +++ b/spec/__snapshots__/aboutApp.spec.ts.snap @@ -189,7 +189,9 @@ ShallowWrapper { "getChildContext": Object { "calledByRenderer": false, }, - "getDerivedStateFromProps": true, + "getDerivedStateFromProps": Object { + "hasShouldComponentUpdateBug": false, + }, "getSnapshotBeforeUpdate": true, "setState": Object { "skipsComponentDidUpdateOnNullish": true, diff --git a/spec/__snapshots__/basicAuth.spec.ts.snap b/spec/__snapshots__/basicAuth.spec.ts.snap index 8f8e077e0..9bd3d22c3 100644 --- a/spec/__snapshots__/basicAuth.spec.ts.snap +++ b/spec/__snapshots__/basicAuth.spec.ts.snap @@ -1117,7 +1117,9 @@ ShallowWrapper { "getChildContext": Object { "calledByRenderer": false, }, - "getDerivedStateFromProps": true, + "getDerivedStateFromProps": Object { + "hasShouldComponentUpdateBug": false, + }, "getSnapshotBeforeUpdate": true, "setState": Object { "skipsComponentDidUpdateOnNullish": true, diff --git a/spec/__snapshots__/downloadManager.spec.ts.snap b/spec/__snapshots__/downloadManager.spec.ts.snap index bec11331b..8e3a08565 100644 --- a/spec/__snapshots__/downloadManager.spec.ts.snap +++ b/spec/__snapshots__/downloadManager.spec.ts.snap @@ -167,7 +167,9 @@ ShallowWrapper { "getChildContext": Object { "calledByRenderer": false, }, - "getDerivedStateFromProps": true, + "getDerivedStateFromProps": Object { + "hasShouldComponentUpdateBug": false, + }, "getSnapshotBeforeUpdate": true, "setState": Object { "skipsComponentDidUpdateOnNullish": true, diff --git a/spec/__snapshots__/loadingScreen.spec.ts.snap b/spec/__snapshots__/loadingScreen.spec.ts.snap index 658869db3..f5aaf7fb9 100644 --- a/spec/__snapshots__/loadingScreen.spec.ts.snap +++ b/spec/__snapshots__/loadingScreen.spec.ts.snap @@ -363,7 +363,9 @@ ShallowWrapper { "getChildContext": Object { "calledByRenderer": false, }, - "getDerivedStateFromProps": true, + "getDerivedStateFromProps": Object { + "hasShouldComponentUpdateBug": false, + }, "getSnapshotBeforeUpdate": true, "setState": Object { "skipsComponentDidUpdateOnNullish": true, diff --git a/spec/__snapshots__/screenPicker.spec.ts.snap b/spec/__snapshots__/screenPicker.spec.ts.snap index 3a2bbc92c..740bf75fd 100644 --- a/spec/__snapshots__/screenPicker.spec.ts.snap +++ b/spec/__snapshots__/screenPicker.spec.ts.snap @@ -577,7 +577,9 @@ ShallowWrapper { "getChildContext": Object { "calledByRenderer": false, }, - "getDerivedStateFromProps": true, + "getDerivedStateFromProps": Object { + "hasShouldComponentUpdateBug": false, + }, "getSnapshotBeforeUpdate": true, "setState": Object { "skipsComponentDidUpdateOnNullish": true, diff --git a/spec/__snapshots__/screenSharingIndicator.spec.ts.snap b/spec/__snapshots__/screenSharingIndicator.spec.ts.snap index 02a9deada..753391495 100644 --- a/spec/__snapshots__/screenSharingIndicator.spec.ts.snap +++ b/spec/__snapshots__/screenSharingIndicator.spec.ts.snap @@ -259,7 +259,9 @@ ShallowWrapper { "getChildContext": Object { "calledByRenderer": false, }, - "getDerivedStateFromProps": true, + "getDerivedStateFromProps": Object { + "hasShouldComponentUpdateBug": false, + }, "getSnapshotBeforeUpdate": true, "setState": Object { "skipsComponentDidUpdateOnNullish": true, diff --git a/spec/__snapshots__/snackBar.spec.ts.snap b/spec/__snapshots__/snackBar.spec.ts.snap new file mode 100644 index 000000000..58824c7c0 --- /dev/null +++ b/spec/__snapshots__/snackBar.spec.ts.snap @@ -0,0 +1,61 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`snack bar should render correctly 1`] = ` +ShallowWrapper { + Symbol(enzyme.__root__): [Circular], + Symbol(enzyme.__unrendered__): , + Symbol(enzyme.__renderer__): Object { + "batchedUpdates": [Function], + "checkPropTypes": [Function], + "getNode": [Function], + "render": [Function], + "simulateError": [Function], + "simulateEvent": [Function], + "unmount": [Function], + }, + Symbol(enzyme.__node__): Object { + "instance": null, + "key": undefined, + "nodeType": "host", + "props": Object {}, + "ref": null, + "rendered": null, + "type": "div", + }, + Symbol(enzyme.__nodes__): Array [ + Object { + "instance": null, + "key": undefined, + "nodeType": "host", + "props": Object {}, + "ref": null, + "rendered": null, + "type": "div", + }, + ], + Symbol(enzyme.__options__): Object { + "adapter": ReactSixteenAdapter { + "options": Object { + "enableComponentDidUpdateOnSetState": true, + "legacyContextMode": "parent", + "lifecycles": Object { + "componentDidUpdate": Object { + "onSetState": true, + }, + "getChildContext": Object { + "calledByRenderer": false, + }, + "getDerivedStateFromProps": Object { + "hasShouldComponentUpdateBug": false, + }, + "getSnapshotBeforeUpdate": true, + "setState": Object { + "skipsComponentDidUpdateOnNullish": true, + }, + }, + }, + }, + }, + Symbol(enzyme.__childContext__): null, +} +`; 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); + }); +});