File size: 1,876 Bytes
f2bee8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from 'react';
import {mount} from 'enzyme';

import ThrottledPropertyHOC from '../../../src/lib/throttled-property-hoc.jsx';

describe('VMListenerHOC', () => {
    let mounted;
    const throttleTime = 500;
    beforeEach(() => {
        const Component = ({propToThrottle, doNotThrottle}) => (
            <input
                name={doNotThrottle}
                value={propToThrottle}
            />
        );
        const WrappedComponent = ThrottledPropertyHOC('propToThrottle', throttleTime)(Component);

        global.Date.now = () => 0;

        mounted = mount(
            <WrappedComponent
                doNotThrottle="oldvalue"
                propToThrottle={0}
            />
        );
    });

    test('it passes the props on initial render ', () => {
        expect(mounted.find('[value=0]').exists()).toEqual(true);
        expect(mounted.find('[name="oldvalue"]').exists()).toEqual(true);
    });

    test('it does not rerender if throttled prop is updated too soon', () => {
        global.Date.now = () => throttleTime / 2;
        mounted.setProps({propToThrottle: 1});
        mounted.update();
        expect(mounted.find('[value=0]').exists()).toEqual(true);
    });

    test('it does rerender if throttled prop is updated after throttle timeout', () => {
        global.Date.now = () => throttleTime * 2;
        mounted.setProps({propToThrottle: 1});
        mounted.update();
        expect(mounted.find('[value=1]').exists()).toEqual(true);
    });

    test('it does rerender if a non-throttled prop is changed', () => {
        global.Date.now = () => throttleTime / 2;
        mounted.setProps({doNotThrottle: 'newvalue', propToThrottle: 2});
        mounted.update();
        expect(mounted.find('[name="newvalue"]').exists()).toEqual(true);
        expect(mounted.find('[value=2]').exists()).toEqual(true);
    });
});