SymphonyElectron/spec/windowsTitleBar.spec.ts

138 lines
5.2 KiB
TypeScript
Raw Normal View History

2019-03-20 00:16:01 -05:00
import { shallow } from 'enzyme';
import * as React from 'react';
import { apiCmds } from '../src/common/api-interface';
2019-03-20 00:16:01 -05:00
import WindowsTitleBar from '../src/renderer/components/windows-title-bar';
import { ipcRenderer } from './__mocks__/electron';
2019-03-20 00:16:01 -05:00
// @ts-ignore
global.MutationObserver = jest.fn().mockImplementation(() => ({
2021-01-29 00:25:40 -06:00
observe: jest.fn(),
disconnect: jest.fn(),
}));
// TODO: Fix tests
2019-03-20 00:16:01 -05:00
describe('windows title bar', () => {
2021-01-29 00:25:40 -06:00
const onEventLabel = 'on';
const sendEventLabel = 'send';
const apiName = 'symphony-api';
2021-01-29 00:25:40 -06:00
const maximizeEventLabel = 'maximize';
const unmaximizeEventLabel = 'unmaximize';
2019-03-20 00:16:01 -05:00
2021-01-29 00:25:40 -06:00
it('should render correctly', () => {
const wrapper = shallow(React.createElement(WindowsTitleBar));
expect(wrapper).toMatchSnapshot();
});
2019-03-20 00:16:01 -05:00
2021-01-29 00:25:40 -06:00
it('should mount correctly', () => {
const spy = jest.spyOn(ipcRenderer, onEventLabel);
2021-01-29 00:25:40 -06:00
const wrapper = shallow(React.createElement(WindowsTitleBar));
const instance: any = wrapper.instance();
instance.updateState({ isMaximized: false });
2021-01-29 00:25:40 -06:00
expect(spy).toBeCalled();
expect(spy).nthCalledWith(1, maximizeEventLabel, expect.any(Function));
expect(spy).nthCalledWith(2, unmaximizeEventLabel, expect.any(Function));
2021-01-29 00:25:40 -06:00
});
2019-03-20 00:16:01 -05:00
2021-01-29 00:25:40 -06:00
it('should call `close` correctly', () => {
const titleLabel = 'Close';
const wrapper = shallow(React.createElement(WindowsTitleBar));
const customSelector = `button.title-bar-button[title="${titleLabel}"]`;
const cmd = {
cmd: apiCmds.closeMainWindow,
};
const spy = jest.spyOn(ipcRenderer, sendEventLabel);
2021-01-29 00:25:40 -06:00
wrapper.find(customSelector).simulate('click');
expect(spy).toBeCalledWith(apiName, cmd);
2021-01-29 00:25:40 -06:00
});
2019-03-20 00:16:01 -05:00
2021-01-29 00:25:40 -06:00
it('should call `minimize` correctly', () => {
const titleLabel = 'Minimize';
const wrapper = shallow(React.createElement(WindowsTitleBar));
const customSelector = `button.title-bar-button[title="${titleLabel}"]`;
const spy = jest.spyOn(ipcRenderer, sendEventLabel);
const cmd = {
cmd: apiCmds.minimizeMainWindow,
};
2021-01-29 00:25:40 -06:00
wrapper.find(customSelector).simulate('click');
expect(spy).toBeCalledWith(apiName, cmd);
2021-01-29 00:25:40 -06:00
});
2019-03-20 00:16:01 -05:00
2021-01-29 00:25:40 -06:00
it('should call `showMenu` correctly', () => {
const titleLabel = 'Menu';
const symphonyApiLabel = 'symphony-api';
const expectedValue = {
cmd: 'popup-menu',
};
const spy = jest.spyOn(ipcRenderer, 'send');
const customSelector = `button.hamburger-menu-button[title="${titleLabel}"]`;
const wrapper = shallow(React.createElement(WindowsTitleBar));
wrapper.find(customSelector).simulate('click');
expect(spy).toBeCalledWith(symphonyApiLabel, expectedValue);
});
it('should call `onMouseDown` correctly', () => {
const titleLabel = 'Menu';
const customSelector = `button.hamburger-menu-button[title="${titleLabel}"]`;
const wrapper = shallow(React.createElement(WindowsTitleBar));
const event = {
preventDefault: jest.fn(),
};
const spy = jest.spyOn(event, 'preventDefault');
wrapper.find(customSelector).simulate('mouseDown', event);
expect(spy).toBeCalled();
});
it('should call `updateState` correctly', () => {
const wrapper = shallow(React.createElement(WindowsTitleBar));
const spy = jest.spyOn(wrapper, 'setState');
const instance: any = wrapper.instance();
instance.updateState({ isMaximized: false });
expect(spy).lastCalledWith(expect.any(Function));
});
describe('maximize functions', () => {
it('should call `unmaximize` correctly when is not full screen', () => {
const titleLabel = 'Restore';
const cmd = {
cmd: apiCmds.unmaximizeMainWindow,
};
2021-01-29 00:25:40 -06:00
const customSelector = `button.title-bar-button[title="${titleLabel}"]`;
const wrapper = shallow(React.createElement(WindowsTitleBar));
const spy = jest.spyOn(ipcRenderer, sendEventLabel);
2021-01-29 00:25:40 -06:00
wrapper.setState({ isMaximized: true });
wrapper.find(customSelector).simulate('click');
expect(spy).toBeCalledWith(apiName, cmd);
2019-03-20 00:16:01 -05:00
});
2021-01-29 00:25:40 -06:00
it('should call `unmaximize` correctly when is full screen', () => {
const titleLabel = 'Restore';
const customSelector = `button.title-bar-button[title="${titleLabel}"]`;
const wrapper = shallow(React.createElement(WindowsTitleBar));
const cmd = {
cmd: apiCmds.unmaximizeMainWindow,
};
const spy = jest.spyOn(ipcRenderer, sendEventLabel);
2021-01-29 00:25:40 -06:00
wrapper.setState({ isMaximized: true });
wrapper.find(customSelector).simulate('click');
expect(spy).toBeCalledWith(apiName, cmd);
2019-03-20 00:16:01 -05:00
});
2021-01-29 00:25:40 -06:00
it('should call maximize correctly when it is not in full screen', () => {
const titleLabel = 'Maximize';
const expectedState = { isMaximized: true };
const customSelector = `button.title-bar-button[title="${titleLabel}"]`;
const wrapper = shallow(React.createElement(WindowsTitleBar));
wrapper.setState({ isMaximized: false });
const spy = jest.spyOn(ipcRenderer, sendEventLabel);
2021-01-29 00:25:40 -06:00
const spyState = jest.spyOn(wrapper, 'setState');
wrapper.find(customSelector).simulate('click');
const cmd = {
cmd: apiCmds.maximizeMainWindow,
};
expect(spy).toBeCalled();
expect(spy).toBeCalledWith(apiName, cmd);
2021-01-29 00:25:40 -06:00
expect(spyState).lastCalledWith(expectedState);
2019-03-20 00:16:01 -05:00
});
2021-01-29 00:25:40 -06:00
});
2019-03-20 00:16:01 -05:00
});