-
Notifications
You must be signed in to change notification settings - Fork 2
/
Spec.js
77 lines (65 loc) · 2.33 KB
/
Spec.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import 'jsdom-global/register';
import React from 'react';
import {
mount,
shallow
} from 'enzyme';
import expect from 'expect';
import sinon from 'sinon';
import {
Autobind
} from '../src';
import ComponentWithoutBinding from './examples/ComponentWithoutBinding';
import ComponentWithOurAutoBind from './examples/ComponentWithOurAutoBind';
import ComponentWithAutoBindDecorator from './examples/ComponentWithAutoBindDecorator';
const ComponentBoundAgain = Autobind(ComponentWithAutoBindDecorator);
describe(`babel-autobind`, () => {
it(`proves that "babel-autobind" solves the issue of binding automatically`, () => {
const wrapper = mount( <ComponentWithOurAutoBind /> );
expect(() => {
wrapper.instance().handleClick({});
}).toNotThrow();
});
it(`proves that using "@autobind decorator" is not enough to stub/spy methods`, () => {
expect(() => {
const stub = sinon.stub(
ComponentWithAutoBindDecorator.prototype,
'handleClick'
).returns(true);
stub.restore();
}).toThrow();
});
it(`proves that binding again with "babel-autobind" solves the issue of spying methods bound by @autobind at the 1st time`, () => {
expect(() => {
const stub = sinon.stub(
ComponentBoundAgain.prototype,
'handleClick'
).returns(true);
stub.restore();
}).toNotThrow();
});
it(`proves that "babel-autobind" is enough to have a successed unit-test`, () => {
const stub = sinon.stub(
ComponentWithOurAutoBind.prototype,
'handleClick'
).returns(true);
const wrapper = mount( <ComponentWithOurAutoBind /> );
wrapper.find('button').simulate('click');
wrapper.find('button').simulate('click');
expect(stub.calledTwice).toBeTruthy();
stub.restore();
});
it(`is possible to do the Autobind after exporting the Component`, () => {
const Proof = Autobind(ComponentWithoutBinding);
const wrapper = mount( < Proof /> );
expect(() => {
wrapper.instance().handleClick({});
}).toNotThrow();
});
it(`rename the bound class to be the same as the name of the original class`, () => {
let Target = Autobind(ComponentWithoutBinding);
expect(Target.name).toNotEqual('ComponentWithoutBinding');
Target = Autobind(ComponentWithoutBinding, true);
expect(Target.name).toEqual('ComponentWithoutBinding');
});
});