Skip to content

Commit 0f95cee

Browse files
committed
starting unit testing on SwitchApp component
1 parent 7f78b7c commit 0f95cee

File tree

4 files changed

+87
-56
lines changed

4 files changed

+87
-56
lines changed

src/app/__tests__/Diff.test.jsx

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React from 'react';
22
import { configure, shallow } from 'enzyme';
33
import Adapter from 'enzyme-adapter-react-16';
4-
import { diff, formatters } from 'jsondiffpatch';
5-
import ReactHtmlParser from 'react-html-parser';
64
import Diff from '../components/Diff.jsx';
75

86
import { useStoreContext } from '../store';
@@ -13,51 +11,32 @@ jest.mock('../store');
1311

1412
describe('Unit testing for Diff.jsx', () => {
1513
let wrapper;
16-
// const props = {
17-
// show: false,
18-
// snapshot: [{
19-
// children: [{
20-
// state: { total: 12, next: 5, operation: null },
21-
// }],
14+
const props = {
15+
show: false,
16+
snapshot: [{
17+
children: [{
18+
state: { total: 12, next: 5, operation: null },
19+
}],
2220

23-
// }],
24-
// }
21+
}],
22+
}
2523

2624
const state = {
2725
currentTab: 100,
28-
tabs: { 100: { snapshots: [1, 2 ,3 ,4 ], viewIndex: 1, sliderIndex: 1 } },
26+
tabs: { 100: { snapshots: [1, 2, 3, 4], viewIndex: 1, sliderIndex: 1 } },
2927
};
3028

31-
const currentTab = state.tabs[state.currentTab];
3229

33-
jest.mock('../store');
3430
useStoreContext.mockImplementation(() => [state]);
3531

3632
const delta = { children: {} }; // expect delta to be an obj
3733
const html = 'html'; // expect html to be a string
3834
const previous = { state: 'string', children: {} }; // expect previous to be an obj
3935

4036
beforeEach(() => {
41-
wrapper = shallow(<Diff />);
42-
useStoreContext.mockClear();
43-
// console.log('Diff component wrapper', wrapper);
37+
wrapper = shallow(<Diff {...props} />);
4438
});
4539

46-
47-
// // const state = {
48-
// // currentTab: 1,
49-
// // tabs: [],
50-
// // };
51-
52-
// const delta = { children: {} }; // expect delta to be an obj
53-
// const html = 'html'; // expect html to be a string
54-
// const previous = { state: 'string', children: {} }; // expect previous to be an obj
55-
56-
// beforeEach(() => {
57-
// wrapper = shallow(<Diff snapshot={props.snapshot} show={props.show} />);
58-
// // console.log('Diff component wrapper', wrapper);
59-
// });
60-
6140
describe('delta', () => {
6241
it('delta variable should be an object, with a property children', () => {
6342
expect(typeof delta).toBe('object');
@@ -79,27 +58,17 @@ describe('Unit testing for Diff.jsx', () => {
7958
});
8059
});
8160

82-
// the diff component should be a diff
83-
// it should detect no state change when state is undefined
8461
describe('Diff Component', () => {
8562
it('Check if Diff component is a div', () => {
8663
expect(wrapper.type()).toEqual('div');
8764
});
88-
it('Check if Diff component inner text states no chages made', () => {
89-
expect(wrapper.text()).toEqual('string');
65+
it('Check if Diff component inner text value is a string', () => {
66+
expect(typeof wrapper.text()).toEqual('string');
67+
});
68+
xit('Check if Diff component div has a className noState ', () => {
69+
console.log(wrapper.props());
70+
expect(wrapper.props().className).toEqual('noState');
9071
});
9172
});
9273

9374
});
94-
// JSX INNER TEXT TEST
95-
// const wrapper = shallow(<div><b>important</b></div>);
96-
// expect(wrapper.text()).to.equal('important');
97-
98-
// need to test that if there is no state change detected, we expect a div to be returned with a className="noState"
99-
100-
// If there is a stateChange we expect a div with the appropriate state changes as an html react element obj that is parsed
101-
102-
// test delta; its initially an array with an obj; as soon as you change state from the initial state change, delta becomes an object
103-
104-
105-
// check for className=noState

src/app/__tests__/SwitchApp.test.jsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
import Select from 'react-select';
3+
import { configure, shallow } from 'enzyme';
4+
import Adapter from 'enzyme-adapter-react-16';
5+
import SwitchApp from '../components/SwitchApp.jsx'
6+
7+
import { useStoreContext } from '../store';
8+
9+
configure({ adapter: new Adapter() });
10+
11+
jest.mock('../store');
12+
13+
14+
describe('Unit testing for SwitchApp.jsx', () => {
15+
let wrapper;
16+
17+
const state = {
18+
currentTab: 100,
19+
tabs: { 100: { snapshots: [1, 2, 3, 4], viewIndex: 1, sliderIndex: 1 } },
20+
};
21+
const tabsArray = [];
22+
const currTab = {
23+
value: 100,
24+
label: {},
25+
};
26+
27+
const dispatch = jest.fn();
28+
useStoreContext.mockImplementation(() => [dispatch, state]);
29+
30+
beforeEach(() => {
31+
wrapper = shallow(<SwitchApp />);
32+
});
33+
34+
describe('currentTab', () => {
35+
it('should have properties value and label', () => {
36+
expect(currTab).toHaveProperty('value');
37+
expect(currTab).toHaveProperty('label');
38+
});
39+
});
40+
41+
// check if currTab has properties value, label
42+
// currentTab should be a number
43+
// tab should be an object
44+
// check if onChange if the function runs
45+
// className should be tab-select-container
46+
// options should be an array
47+
// value prop should be equal to a number
48+
})

src/app/components/Diff.jsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,42 @@ import PropTypes from 'prop-types';
33
import { diff, formatters } from 'jsondiffpatch';
44
import ReactHtmlParser from 'react-html-parser';
55

6-
import { useStoreContext } from '../store'
6+
import { useStoreContext } from '../store';
77

88
function Diff({ snapshot, show }) {
99
const [mainState] = useStoreContext();
10+
// console.log('this is the mainState variable -->', mainState);
1011
const { currentTab, tabs } = mainState; // Nate:: k/v pairs of mainstate store object being created
12+
// console.log('this is the currentTab variable -->', currentTab);
13+
// console.log('this is the tabs variable -->', tabs);
1114
const { snapshots, viewIndex, sliderIndex } = tabs[currentTab];
1215
let previous;
1316

1417
// previous follows viewIndex or sliderIndex
15-
if (viewIndex !== -1) {
18+
if (viewIndex !== -1) { // if tab isnt selected, view index is set to -1
1619
previous = snapshots[viewIndex - 1];
20+
// console.log('previous with viewIndex - 1: ', previous)
1721
} else {
1822
previous = snapshots[sliderIndex - 1];
23+
// console.log('previous with sliderIndex - 1: ', previous)
1924
}
2025
// Nate:: diff function returns a comparaison of two objects, one has an updated change
21-
const delta = diff(previous, snapshot);
26+
const delta = diff(previous, snapshot); // initilly an array with one object, on state change becomes an object
27+
// console.log('delta: ', delta);
2228
// returns html in string
2329
const html = formatters.html.format(delta, previous);
24-
if (show) formatters.html.showUnchanged();
25-
else formatters.html.hideUnchanged();
26-
30+
// console.log('html -->', html)
31+
if (show){
32+
// console.log('showing unchanged values', formatters.html.showUnchanged() );
33+
formatters.html.showUnchanged();
34+
} else {
35+
// console.log('hiding unchanged values', formatters.html.hideUnchanged() );
36+
formatters.html.hideUnchanged();
37+
};
38+
2739
if (previous === undefined || delta === undefined) {
28-
return <div> No state change detected. Trigger an event to change state </div>;
40+
console.log('reacthtml parser -->', ReactHtmlParser(html), typeof ReactHtmlParser(html))
41+
return <div className='noState'> No state change detected. Trigger an event to change state </div>;
2942
}
3043
return <div>{ReactHtmlParser(html)}</div>;
3144
}

src/app/components/SwitchApp.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import { setTab } from '../actions/actions';
66

77
const SwitchAppDropdown = () => {
88
const [{ currentTab, tabs }, dispatch] = useStoreContext();
9-
9+
console.log('tabs object -->', tabs);
10+
1011
const tabsArray = [];
11-
1212
Object.keys(tabs).forEach(tab => {
1313
tabsArray.push({ value: tab, label: tabs[tab].title });
1414
});
15+
console.log('tabs object -->', tabsArray);
1516

1617
const currTab = {
1718
value: currentTab,

0 commit comments

Comments
 (0)