Skip to content

Commit 2732718

Browse files
43081jJoviDeCroock
andauthored
test: migrate context tests to vitest spies (#4810)
Co-authored-by: Jovi De Croock <[email protected]>
1 parent 443e688 commit 2732718

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

test/browser/context.test.js

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('context', () => {
3232
);
3333
}
3434
}
35-
sinon.spy(Outer.prototype, 'getChildContext');
35+
vi.spyOn(Outer.prototype, 'getChildContext');
3636

3737
class Inner extends Component {
3838
// constructor() {
@@ -49,42 +49,45 @@ describe('context', () => {
4949
return <div>{context && context.a}</div>;
5050
}
5151
}
52-
sinon.spy(Inner.prototype, 'shouldComponentUpdate');
53-
sinon.spy(Inner.prototype, 'componentWillReceiveProps');
54-
sinon.spy(Inner.prototype, 'componentWillUpdate');
55-
sinon.spy(Inner.prototype, 'componentDidUpdate');
56-
sinon.spy(Inner.prototype, 'render');
52+
vi.spyOn(Inner.prototype, 'shouldComponentUpdate');
53+
vi.spyOn(Inner.prototype, 'componentWillReceiveProps');
54+
vi.spyOn(Inner.prototype, 'componentWillUpdate');
55+
vi.spyOn(Inner.prototype, 'componentDidUpdate');
56+
vi.spyOn(Inner.prototype, 'render');
5757

5858
render(<Outer />, scratch);
5959

60-
expect(Outer.prototype.getChildContext).to.have.been.calledOnce;
60+
expect(Outer.prototype.getChildContext).toHaveBeenCalledTimes(1);
6161

6262
// initial render does not invoke anything but render():
63-
expect(Inner.prototype.render).to.have.been.calledWith({}, {}, CONTEXT);
63+
expect(Inner.prototype.render).toHaveBeenCalledWith({}, {}, CONTEXT);
6464

6565
CONTEXT.foo = 'bar';
6666
render(<Outer {...PROPS} />, scratch);
6767

68-
expect(Outer.prototype.getChildContext).to.have.been.calledTwice;
68+
expect(Outer.prototype.getChildContext).toHaveBeenCalledTimes(2);
6969

70-
expect(
71-
Inner.prototype.shouldComponentUpdate
72-
).to.have.been.calledOnce.and.calledWith(PROPS, {}, CONTEXT);
73-
expect(Inner.prototype.componentWillReceiveProps).to.have.been.calledWith(
70+
expect(Inner.prototype.shouldComponentUpdate).toHaveBeenCalledTimes(1);
71+
expect(Inner.prototype.shouldComponentUpdate).toHaveBeenCalledWith(
72+
PROPS,
73+
{},
74+
CONTEXT
75+
);
76+
expect(Inner.prototype.componentWillReceiveProps).toHaveBeenCalledWith(
7477
PROPS,
7578
CONTEXT
7679
);
77-
expect(Inner.prototype.componentWillUpdate).to.have.been.calledWith(
80+
expect(Inner.prototype.componentWillUpdate).toHaveBeenCalledWith(
7881
PROPS,
7982
{},
8083
CONTEXT
8184
);
82-
expect(Inner.prototype.componentDidUpdate).to.have.been.calledWith(
85+
expect(Inner.prototype.componentDidUpdate).toHaveBeenCalledWith(
8386
{},
8487
{},
8588
undefined
8689
);
87-
expect(Inner.prototype.render).to.have.been.calledWith(PROPS, {}, CONTEXT);
90+
expect(Inner.prototype.render).toHaveBeenCalledWith(PROPS, {}, CONTEXT);
8891

8992
/* Future:
9093
* Newly created context objects are *not* currently cloned.
@@ -108,7 +111,7 @@ describe('context', () => {
108111
return <Inner {...props} />;
109112
}
110113
}
111-
sinon.spy(Outer.prototype, 'getChildContext');
114+
vi.spyOn(Outer.prototype, 'getChildContext');
112115

113116
class Inner extends Component {
114117
shouldComponentUpdate() {
@@ -121,46 +124,49 @@ describe('context', () => {
121124
return <div>{context && context.a}</div>;
122125
}
123126
}
124-
sinon.spy(Inner.prototype, 'shouldComponentUpdate');
125-
sinon.spy(Inner.prototype, 'componentWillReceiveProps');
126-
sinon.spy(Inner.prototype, 'componentWillUpdate');
127-
sinon.spy(Inner.prototype, 'componentDidUpdate');
128-
sinon.spy(Inner.prototype, 'render');
127+
vi.spyOn(Inner.prototype, 'shouldComponentUpdate');
128+
vi.spyOn(Inner.prototype, 'componentWillReceiveProps');
129+
vi.spyOn(Inner.prototype, 'componentWillUpdate');
130+
vi.spyOn(Inner.prototype, 'componentDidUpdate');
131+
vi.spyOn(Inner.prototype, 'render');
129132

130133
render(<Outer />, scratch);
131134

132-
expect(Outer.prototype.getChildContext).to.have.been.calledOnce;
135+
expect(Outer.prototype.getChildContext).toHaveBeenCalledTimes(1);
133136

134137
// initial render does not invoke anything but render():
135-
expect(Inner.prototype.render).to.have.been.calledWith({}, {}, CONTEXT);
138+
expect(Inner.prototype.render).toHaveBeenCalledWith({}, {}, CONTEXT);
136139

137140
CONTEXT.foo = 'bar';
138141
render(<Outer {...PROPS} />, scratch);
139142

140-
expect(Outer.prototype.getChildContext).to.have.been.calledTwice;
143+
expect(Outer.prototype.getChildContext).toHaveBeenCalledTimes(2);
141144

142-
expect(
143-
Inner.prototype.shouldComponentUpdate
144-
).to.have.been.calledOnce.and.calledWith(PROPS, {}, CONTEXT);
145-
expect(Inner.prototype.componentWillReceiveProps).to.have.been.calledWith(
145+
expect(Inner.prototype.shouldComponentUpdate).toHaveBeenCalledTimes(1);
146+
expect(Inner.prototype.shouldComponentUpdate).toHaveBeenCalledWith(
147+
PROPS,
148+
{},
149+
CONTEXT
150+
);
151+
expect(Inner.prototype.componentWillReceiveProps).toHaveBeenCalledWith(
146152
PROPS,
147153
CONTEXT
148154
);
149-
expect(Inner.prototype.componentWillUpdate).to.have.been.calledWith(
155+
expect(Inner.prototype.componentWillUpdate).toHaveBeenCalledWith(
150156
PROPS,
151157
{},
152158
CONTEXT
153159
);
154-
expect(Inner.prototype.componentDidUpdate).to.have.been.calledWith(
160+
expect(Inner.prototype.componentDidUpdate).toHaveBeenCalledWith(
155161
{},
156162
{},
157163
undefined
158164
);
159-
expect(Inner.prototype.render).to.have.been.calledWith(PROPS, {}, CONTEXT);
165+
expect(Inner.prototype.render).toHaveBeenCalledWith(PROPS, {}, CONTEXT);
160166

161167
// make sure render() could make use of context.a
162-
expect(Inner.prototype.render).to.have.returned(
163-
sinon.match({ props: { children: 'a' } })
168+
expect(Inner.prototype.render).toHaveReturned(
169+
expect.objectContaining({ props: { children: 'a' } })
164170
);
165171
});
166172

@@ -195,17 +201,17 @@ describe('context', () => {
195201
}
196202
}
197203

198-
sinon.spy(Inner.prototype, 'render');
199-
sinon.spy(InnerMost.prototype, 'render');
204+
vi.spyOn(Inner.prototype, 'render');
205+
vi.spyOn(InnerMost.prototype, 'render');
200206

201207
render(<Outer />, scratch);
202208

203-
expect(Inner.prototype.render).to.have.been.calledWith(
209+
expect(Inner.prototype.render).toHaveBeenCalledWith(
204210
{},
205211
{},
206212
{ outerContext }
207213
);
208-
expect(InnerMost.prototype.render).to.have.been.calledWith(
214+
expect(InnerMost.prototype.render).toHaveBeenCalledWith(
209215
{},
210216
{},
211217
{ outerContext, innerContext }

0 commit comments

Comments
 (0)