1
1
import { createRenderer , renderIntoDocument , findRenderedComponentWithType } from 'react-addons-test-utils' ;
2
2
import { expect } from 'chai' ;
3
+ import sinon from 'sinon' ;
3
4
import React , { Component } from 'react' ;
4
5
import h from 'react-hyperscript' ;
5
6
import timer from './timer' ;
@@ -11,27 +12,55 @@ const win = doc.defaultView;
11
12
global . document = doc ;
12
13
global . window = win ;
13
14
15
+ class Counter extends Component {
16
+ render ( ) {
17
+ const { tick } = this . props ;
18
+ return h ( 'div' , { tick } ) ;
19
+ }
20
+ }
21
+
14
22
describe ( 'Timer' , function ( ) {
15
- it ( 'should run' , function ( ) {
16
- class Counter extends Component {
17
- render ( ) {
18
- const { tick } = this . props ;
19
- return h ( 'div' , { tick } ) ;
20
- }
21
- }
23
+ let clock , wrappedCounter , counter ;
24
+
25
+ before ( ( ) => clock = sinon . useFakeTimers ( ) ) ;
26
+ after ( ( ) => clock . restore ( ) ) ;
22
27
28
+ it ( 'should pass down a timer property alongside other props' , function ( ) {
23
29
const WrappedCounter = timer ( 1000 ) ( Counter ) ;
24
30
expect ( WrappedCounter . displayName ) . to . equal ( 'Timer@1000[Counter]' ) ;
25
31
26
32
const wrappedCounter = renderIntoDocument ( h ( WrappedCounter , { customProp : 1 } ) ) ;
27
- const counter = findRenderedComponentWithType ( wrappedCounter , Counter ) ;
28
- expect ( counter . props . tick ) . to . equal ( 0 ) ;
29
- expect ( counter . props . delay ) . to . equal ( 1000 ) ;
30
- expect ( counter . props . stop ) . to . be . a . function ;
33
+
34
+ counter = findRenderedComponentWithType ( wrappedCounter , Counter ) ;
35
+
36
+ expect ( counter . props . timer . tick ) . to . equal ( 0 ) ;
37
+ expect ( counter . props . timer . delay ) . to . equal ( 1000 ) ;
38
+ expect ( counter . props . timer . stop ) . to . be . a . function ;
39
+ expect ( counter . props . timer . setDelay ) . to . be . a . function ;
31
40
expect ( counter . props . customProp ) . to . equal ( 1 ) ;
41
+ } ) ;
32
42
43
+ it ( 'should increment a tick property' , function ( ) {
44
+ clock . tick ( 1100 ) ;
45
+ expect ( counter . props . timer . tick ) . to . equal ( 1 ) ;
46
+ clock . tick ( 1000 ) ;
47
+ expect ( counter . props . timer . tick ) . to . equal ( 2 ) ;
48
+ } ) ;
49
+
50
+ it ( 'should have the ability to be stopped and resumed' , function ( ) {
33
51
expect ( wrappedCounter . stopped ) . to . be . false ;
34
- counter . props . stop ( ) ;
52
+ counter . props . timer . stop ( ) ;
35
53
expect ( wrappedCounter . stopped ) . to . be . true ;
54
+ counter . props . timer . resume ( ) ;
55
+ expect ( wrappedCounter . stopped ) . to . be . false ;
56
+ } ) ;
57
+
58
+ it ( 'shoud give the ability to change its delay' , function ( ) {
59
+ counter . props . timer . setDelay ( 60000 ) ;
60
+ expect ( wrappedCounter . delay ) . to . be . equal ( 60000 ) ;
61
+
62
+ clock . tick ( 60100 ) ;
63
+ expect ( counter . props . timer . tick ) . to . equal ( 3 ) ;
64
+ counter . props . timer . stop ( ) ;
36
65
} ) ;
37
66
} ) ;
0 commit comments