1+ jest . mock ( "../actions" ) ;
2+
3+ import * as React from "react" ;
4+ import { shallow , ShallowWrapper } from "enzyme" ;
5+ import { Dispatch } from "react-redux" ;
6+ import { ActionFunction0 , Action } from "redux-actions" ;
7+
8+ import { Hello , HelloProps , mapDispatchToProps , mapStateToProps } from "../Hello" ;
9+ import Counter from "../containers/Counter" ;
10+ import { IRootState } from "../../rootReducer" ;
11+ import { incrementAction , decrementAction } from "../actions" ;
12+
13+
14+ describe ( "Hello Component" , ( ) => {
15+ let helloComponent : ShallowWrapper < Hello , any > ;
16+ let props : HelloProps ;
17+ let increment : ActionFunction0 < Action < void > > ;
18+ let decrement : ActionFunction0 < Action < void > > ;
19+
20+ beforeEach ( ( ) => {
21+ increment = jest . fn ( ) ;
22+ decrement = jest . fn ( ) ;
23+ props = {
24+ counter : 2 ,
25+ actions : {
26+ incrementAction : increment ,
27+ decrementAction : decrement
28+ }
29+ } as HelloProps ;
30+
31+ helloComponent = shallow ( < Hello actions = { props . actions } counter = { props . counter } /> ) ;
32+ } ) ;
33+
34+ it ( "says hello" , ( ) => {
35+ expect ( helloComponent . find ( "h1" ) . text ( ) ) . toContain ( "Hello typescript and react!" ) ;
36+ } ) ;
37+
38+ describe ( "Counter component" , ( ) => {
39+ it ( "shows the counter component" , ( ) => {
40+ expect ( helloComponent . find ( Counter ) . length ) . toEqual ( 1 ) ;
41+ } ) ;
42+
43+ it ( "passes counter property" , ( ) => {
44+ expect ( helloComponent . find ( Counter ) . props ( ) . counter ) . toEqual ( 2 ) ;
45+ } ) ;
46+
47+ it ( "passes functions of increment the counter" , ( ) => {
48+ helloComponent . find ( Counter ) . props ( ) . increment ( ) ;
49+ expect ( increment ) . toBeCalled ( ) ;
50+ } ) ;
51+
52+ it ( "passes functions of decrement the counter" , ( ) => {
53+ helloComponent . find ( Counter ) . props ( ) . decrement ( ) ;
54+ expect ( decrement ) . toBeCalled ( ) ;
55+ } ) ;
56+ } ) ;
57+
58+ describe ( "mapStateToProps" , ( ) => {
59+ it ( "maps the counter from the state" , ( ) => {
60+ const counters : any = {
61+ counter : 1
62+ } ;
63+
64+ const state : IRootState = {
65+ counters : counters
66+ } ;
67+
68+ let props = mapStateToProps ( state ) ;
69+
70+ expect ( props . counter ) . toBe ( 1 ) ;
71+ } ) ;
72+ } ) ;
73+
74+ describe ( "mapDispatchToProps" , ( ) => {
75+ it ( "maps action functions to decrement the counter" , ( ) => {
76+ decrementAction . mockReturnValue ( "you are going down, sorry!" ) ;
77+ let dispatch : Dispatch < { } > = jest . fn ( ) ;
78+
79+ let props = mapDispatchToProps ( dispatch ) ;
80+ props . actions . decrementAction ( ) ;
81+
82+ expect ( decrementAction ) . toBeCalled ( ) ;
83+ expect ( dispatch ) . toBeCalledWith ( "you are going down, sorry!" ) ;
84+ } ) ;
85+
86+ it ( "maps action functions to increment the counter" , ( ) => {
87+ incrementAction . mockReturnValue ( "you are going up, congratulations!" ) ;
88+ let dispatch : Dispatch < { } > = jest . fn ( ) ;
89+
90+ let props = mapDispatchToProps ( dispatch ) ;
91+ props . actions . incrementAction ( ) ;
92+
93+ expect ( incrementAction ) . toBeCalled ( ) ;
94+ expect ( dispatch ) . toBeCalledWith ( "you are going up, congratulations!" ) ;
95+ } ) ;
96+ } ) ;
97+ } ) ;
0 commit comments