This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
/
component-with-spy.tsx
59 lines (48 loc) · 1.74 KB
/
component-with-spy.tsx
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
56
57
58
59
import expect, { createSpy, spyOn } from 'expect';
import React from 'react';
import { shallow } from 'enzyme';
import { add } from '../src/function';
import { Add } from '../src/component-with-add-prop';
describe('component: `<Add />` with spy', () => {
it('should show the sum (using `createSpy`)', () => {
const addSpy = createSpy().andCall(add);
// const addSpy = createSpy().andReturn(3);
const wrapper = shallow(<Add a={1} b={2} add={addSpy as any} />);
expect(wrapper.type()).toBe('p');
expect(wrapper.text()).toBe('The sum is: 3.');
expect(addSpy).toHaveBeenCalled();
expect(addSpy).toHaveBeenCalledWith(1, 2);
});
it('should show the sum (using `spyOn`)', () => {
const props = {
a: 1,
b: 2,
add
};
const addSpy = spyOn(props, 'add').andCallThrough();
const wrapper = shallow(<Add {...props} />);
expect(wrapper.type()).toBe('p');
expect(wrapper.text()).toBe('The sum is: 3.');
expect(addSpy.calls.length).toEqual(1);
expect(addSpy).toHaveBeenCalledWith(1, 2);
});
});
describe('component: `<Add />` with spy resets', () => {
const addSpy = createSpy().andCall(add);
// beforeEach(() => {});
it('should show the sum for 1 + 2', () => {
const wrapper = shallow(<Add a={1} b={2} add={addSpy as any} />);
expect(wrapper.text()).toBe('The sum is: 3.');
expect(addSpy.calls.length).toEqual(1);
expect(addSpy).toHaveBeenCalledWith(1, 2);
});
it('should show the sum for 2 + 2', () => {
const wrapper = shallow(<Add a={2} b={2} add={addSpy as any} />);
expect(wrapper.text()).toBe('The sum is: 4.');
expect(addSpy.calls.length).toEqual(1);
expect(addSpy).toHaveBeenCalledWith(2, 2);
});
afterEach(() => {
addSpy.reset();
});
});