Skip to content

Commit d1d65fb

Browse files
committed
stopped mocking react and mocked chrome api instead
1 parent 5cb313a commit d1d65fb

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/app/__tests__/MainContainer.test.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable react/jsx-filename-extension */
22

3-
import { shallow, configure } from 'enzyme';
4-
import React, { useState } from 'react';
3+
import { mount, configure } from 'enzyme';
4+
import React from 'react';
5+
import { act } from 'react-dom/test-utils';
56
import Adapter from 'enzyme-adapter-react-16';
67
import MainContainer from '../containers/MainContainer';
78
import { useStoreContext } from '../store';
@@ -19,9 +20,10 @@ configure({ adapter: new Adapter() });
1920
const state = {
2021
tabs: {
2122
87: {
22-
snapshots: [1, 2, 3, 4],
23+
snapshots: [{}, {}, {}, {}],
2324
sliderIndex: 0,
2425
viewIndex: -1,
26+
mode: {},
2527
},
2628
},
2729
currentTab: 87,
@@ -32,16 +34,22 @@ jest.mock('../store');
3234
useStoreContext.mockImplementation(() => [state, dispatch]);
3335

3436
let wrapper;
35-
// global.chrome = chrome;
36-
const setnpm = jest.fn();
37-
38-
37+
global.chrome = chrome;
38+
let eventListener;
39+
const port = {
40+
onMessage: {
41+
addListener: ((fn) => {
42+
eventListener = fn;
43+
}),
44+
},
45+
onDisconnect: {
46+
addListener: () => { },
47+
},
48+
};
49+
chrome.runtime.connect.returns(port);
3950

4051
beforeEach(() => {
41-
wrapper = shallow(<MainContainer />);
42-
chrome.runtime.connect = () => {}
43-
console.log(chrome.runtime.connect);
44-
chrome.runtime.onMessage.dispatch({action: 'initialConnectSnapshots', payload: []});
52+
wrapper = mount(<MainContainer />);
4553
useStoreContext.mockClear();
4654
dispatch.mockClear();
4755
});
@@ -56,6 +64,14 @@ describe('MainContainer rendering', () => {
5664
expect(wrapper.find(ButtonsContainer).length).toBe(0);
5765
});
5866
test('With connection established, should render all containers', () => {
67+
// fake connect
68+
act(() => {
69+
eventListener({
70+
action: 'initialConnectSnapshots',
71+
payload: 'test',
72+
});
73+
});
74+
wrapper.update();
5975
expect(wrapper.find(HeadContainer).length).toBe(1);
6076
expect(wrapper.find(ActionContainer).length).toBe(1);
6177
expect(wrapper.find(StateContainer).length).toBe(1);

src/app/containers/MainContainer.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ function MainContainer() {
3232
break;
3333
}
3434
case 'initialConnectSnapshots': {
35-
console.log('connection received')
3635
dispatch(initialConnect(payload));
3736
setnpm(true);
3837
break;

0 commit comments

Comments
 (0)