add some MovieSettings unit tests

This commit is contained in:
lukas 2021-05-01 15:57:58 +02:00
parent 5fac3a0780
commit d6fd2cbd9c
2 changed files with 48 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import {shallow} from 'enzyme'; import {shallow} from 'enzyme';
import React from 'react'; import React from 'react';
import MovieSettings from './MovieSettings'; import MovieSettings from './MovieSettings';
import {callAPI} from '../../utils/Api';
describe('<MovieSettings/>', function () { describe('<MovieSettings/>', function () {
it('renders without crashing ', function () { it('renders without crashing ', function () {
@ -22,12 +23,55 @@ describe('<MovieSettings/>', function () {
}); });
it('test simulate reindex', function () { it('test simulate reindex', function () {
global.fetch = global.prepareFetchApi({success: true}); callAPIMock({success: true})
const wrapper = shallow(<MovieSettings/>); const wrapper = shallow(<MovieSettings/>);
wrapper.find('button').findWhere(e => e.text() === 'Reindex Movie' && e.type() === 'button').simulate('click'); wrapper.find('button').findWhere(e => e.text() === 'Reindex Movie' && e.type() === 'button').simulate('click');
// initial send of reindex request to server // initial send of reindex request to server
expect(global.fetch).toBeCalledTimes(1); expect(callAPI).toBeCalledTimes(1);
});
it('test simulate tvshow reindex', function () {
callAPIMock({success: true})
const wrapper = shallow(<MovieSettings/>);
wrapper.find('button').findWhere(e => e.text() === 'TVShow Reindex' && e.type() === 'button').simulate('click');
// initial send of reindex request to server
expect(callAPI).toBeCalledTimes(1);
});
it('test handlemessage ', function () {
const wrapper = shallow(<MovieSettings/>);
const func = jest.fn((str) => {})
wrapper.instance().appendLog = func
wrapper.instance().handleMessage('{"Action":"message", "Message":"testmsg"}')
expect(func).toHaveBeenCalledTimes(1);
expect(func).toHaveBeenLastCalledWith('testmsg')
wrapper.setState({startbtnDisabled: false});
// expect button to get disabled!
wrapper.instance().handleMessage('{"Action":"reindexAction", "Event":"start"}');
expect(wrapper.state().startbtnDisabled).toBeTruthy()
// expect button to get enabled
wrapper.instance().handleMessage('{"Action":"reindexAction", "Event":"stop"}');
expect(wrapper.state().startbtnDisabled).not.toBeTruthy()
});
it('test appendlog', function () {
const wrapper = shallow(<MovieSettings/>);
wrapper.instance().appendLog("testmsg");
expect(wrapper.state().text).toHaveLength(1)
expect(wrapper.state().text[0]).toBe('testmsg')
wrapper.instance().appendLog("testmsg2");
expect(wrapper.state().text).toHaveLength(2)
expect(wrapper.state().text[0]).toBe('testmsg2')
expect(wrapper.state().text[1]).toBe('testmsg')
}); });
}); });

View File

@ -44,7 +44,6 @@ class MovieSettings extends React.Component<Props, state> {
dial(): void { dial(): void {
console.log('trying to connect...'); console.log('trying to connect...');
const conn = new WebSocket(`ws://${window.location.host}/subscribe`); const conn = new WebSocket(`ws://${window.location.host}/subscribe`);
console.log('dd to connect...');
conn.addEventListener('close', (ev) => { conn.addEventListener('close', (ev) => {
this.appendLog(`WebSocket Disconnected code: ${ev.code}, reason: ${ev.reason}`, true); this.appendLog(`WebSocket Disconnected code: ${ev.code}, reason: ${ev.reason}`, true);
if (ev.code !== 1001) { if (ev.code !== 1001) {
@ -58,7 +57,6 @@ class MovieSettings extends React.Component<Props, state> {
// This is where we handle messages received. // This is where we handle messages received.
conn.addEventListener('message', (ev) => { conn.addEventListener('message', (ev) => {
console.log('new message!');
if (typeof ev.data !== 'string') { if (typeof ev.data !== 'string') {
console.error('unexpected message type', typeof ev.data); console.error('unexpected message type', typeof ev.data);
return; return;
@ -80,6 +78,8 @@ class MovieSettings extends React.Component<Props, state> {
} else if (msg.Event === 'stop') { } else if (msg.Event === 'stop') {
this.setState({startbtnDisabled: false}); this.setState({startbtnDisabled: false});
} }
} else {
console.error('unexpected response from server: ' + message);
} }
} }