1- import React from 'react' ;
2- import Chart from '../components/Chart' ;
3-
4- // Unit test cases for d3 functionality
1+ import React from 'react' ;
2+ import { configure , mount } from 'enzyme' ;
3+ import Adapter from 'enzyme-adapter-react-16' ;
4+ // eslint-disable-next-line import/no-named-as-default-member
5+ import Chart from '../components/Chart' ;
6+ // Unit test cases for d3 functionality
7+ configure ( { adapter : new Adapter ( ) } ) ;
58// Test the life cycle methods in Chart
69describe ( 'Life cycle methods in Chart' , ( ) => {
10+ let wrapper ;
11+ const props = {
12+ hierarchy : 0 ,
13+ } ;
14+ // Set up wrapper
15+ beforeEach ( ( ) => {
16+ wrapper = mount ( < Chart { ...props } /> ) ;
17+ } ) ;
18+ // test componentDidMount
19+ it ( 'should call componentDidMount once' , ( ) => {
20+ const instance = wrapper . instance ( ) ;
21+ jest . spyOn ( instance , 'componentDidMount' ) ;
22+ instance . componentDidMount ( ) ;
23+ expect ( instance . componentDidMount ) . toHaveBeenCalledTimes ( 1 ) ;
24+ } ) ;
25+ // test maked3Tree within componentDidMount
726 it ( 'should call maked3Tree upon mounting' , ( ) => {
8- // Component should call maked3Tree upon mounting
9- // Use the mock function + .toBeCalled()
10- // Test the method in the component
11- throw new Error ( ) ;
12- } ) ;
13- } ) ;
14-
15- // Test the root object and hierarchy
27+ const instance = wrapper . instance ( ) ;
28+ jest . spyOn ( instance , 'maked3Tree' ) ;
29+ instance . componentDidMount ( ) ;
30+ expect ( instance . maked3Tree ) . toHaveBeenCalledTimes ( 1 ) ;
31+ } ) ;
32+ // test componentDidUpdate
33+ it ( 'should call componentDidUpdate once' , ( ) => {
34+ const instance = wrapper . instance ( ) ;
35+ jest . spyOn ( instance , 'componentDidUpdate' ) ;
36+ instance . componentDidUpdate ( ) ;
37+ expect ( instance . componentDidUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
38+ } ) ;
39+ // test maked3Tree within componentDidUpdate
40+ it ( 'should call maked3Tree once upon updating' , ( ) => {
41+ const instance = wrapper . instance ( ) ;
42+ jest . spyOn ( instance , 'maked3Tree' ) ;
43+ instance . componentDidUpdate ( ) ;
44+ expect ( instance . maked3Tree ) . toHaveBeenCalledTimes ( 1 ) ;
45+ } ) ;
46+ } ) ;
47+ // Test the root object and hierarchy
1648describe ( 'Root object' , ( ) => {
49+ let wrapper ;
50+ const root = { } ;
51+ const props = {
52+ hierarchy : {
53+ index : 1 ,
54+ stateSnapshot : { } ,
55+ children : [ ] ,
56+ } ,
57+ } ;
58+ // Set up wrapper
59+ beforeEach ( ( ) => {
60+ wrapper = mount ( < Chart { ...props } /> ) ;
61+ } ) ;
62+
1763 it ( 'should be a deep clone of the hierarchy' , ( ) => {
18- // object 'root' should be a deep clone of the hierarchy
19- // create an empty object to mimic root
20- // i.e.: this.props.hierarchy !== root
21- // expect to !== this.props.hierarchy
22- // expect to be a deep clone
23- throw new Error ( ) ;
24- } ) ;
25- } ) ;
64+ const instance = wrapper . instance ( ) ;
65+ instance . componentDidMount ( ) ;
66+ expect ( typeof root ) . toBe ( typeof props . hierarchy ) ;
67+ expect ( root ) . not . toEqual ( props . hierarchy ) ;
68+ } ) ;
69+ } ) ;
2670
27- // Test the maked3Tree method
71+ // Test the maked3Tree method
2872describe ( 'maked3Tree method' , ( ) => {
29- it ( 'should call removed3tree once' , ( ) => {
30- // Should call function 'removed3tree' only once
31- // expect toBeCalled()
32- // expect toHaveBeenCalled()
33- throw new Error ( ) ;
34- } ) ;
35- } ) ;
36- // Test the tooltip functionality in maked3Tree method
37- describe ( 'tooltip functionality in maked3Tree' , ( ) => {
38- // Should call appropriate function upon triggering
39- // a certain event on the tooltip div
40- // Should call appropriate function upon triggering mouseOver
41- it ( 'should invoke tip.show on mouseOver' , ( ) => {
42- throw new Error ( ) ;
43- } ) ;
44- // Should call appropriate function upon triggering mouseOut
45- it ( 'should invoke tip.hide on mouseOut' , ( ) => {
46- throw new Error ( ) ;
47- } ) ;
48- } ) ;
49-
50-
51-
52-
73+ let wrapper ;
74+ const props = {
75+ hierarchy : 0 ,
76+ } ;
77+ // Set up wrapper
78+ beforeEach ( ( ) => {
79+ wrapper = mount ( < Chart { ...props } /> ) ;
80+ } ) ;
81+ // Test the invocation of removed3Tree within maked3Tree
82+ it ( 'should call removed3Tree once' , ( ) => {
83+ const instance = wrapper . instance ( ) ;
84+ jest . spyOn ( instance , 'removed3Tree' ) ;
85+ instance . maked3Tree ( ) ;
86+ expect ( instance . removed3Tree ) . toHaveBeenCalledTimes ( 1 ) ;
87+ } ) ;
88+ } ) ;
0 commit comments