Skip to content

Commit

Permalink
[Tests] add expectArgs helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 20, 2020
1 parent 6da8640 commit 9c92f91
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 342 deletions.
19 changes: 19 additions & 0 deletions packages/enzyme-test-suite/test/_helpers/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import React from 'react';
import { Memo } from 'react-is';
import { compareNodeTypeOf } from 'enzyme-adapter-utils';
import sinon from 'sinon-sandbox';
import { expect } from 'chai';

/**
* Simple wrapper around mocha describe which allows a boolean to be passed in first which
Expand Down Expand Up @@ -164,3 +166,20 @@ export function delay(ms) {
export function isMemo(type) {
return compareNodeTypeOf(type, Memo);
}

export function argSpy() {
const spy = sinon.spy();
spy(1);
return spy;
}

export function expectArgs(spy, counter, args) {
spy(counter);
expect(spy.args).to.deep.equal([
[counter],
...args,
[counter],
]);
spy.resetHistory();
spy(counter + 1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from '../../_helpers/react-compat';
import {
describeIf,
argSpy,
expectArgs,
} from '../../_helpers';
import {
is,
Expand Down Expand Up @@ -152,8 +154,7 @@ export default function describeCDU({
describeIf(is('>= 15.3'), 'PureComponent', () => {
let spy;
beforeEach(() => {
spy = sinon.spy();
spy(1);
spy = argSpy();
});

it('does not update when state and props did not change', () => {
Expand All @@ -177,52 +178,27 @@ export default function describeCDU({
}

const wrapper = Wrap(<Foo id={1} />);
spy(1);
expect(spy.args).to.deep.equal([
[1],
expectArgs(spy, 1, [
['render'],
[1],
]);
spy.resetHistory();
spy(2);

wrapper.setState({ foo: 'update' });
spy(2);
expect(spy.args).to.deep.equal([
[2],
expectArgs(spy, 2, [
['render'],
['componentDidUpdate'],
[2],
]);
spy.resetHistory();
spy(3);

wrapper.setState({ foo: 'update' });
spy(3);
expect(spy.args).to.deep.equal([
[3],
[3],
]);
spy.resetHistory();
spy(4);
expectArgs(spy, 3, []);

wrapper.setProps({ id: 2 });
spy(4);
expect(spy.args).to.deep.equal([
[4],
expectArgs(spy, 4, [
['render'],
['componentDidUpdate'],
[4],
]);
spy.resetHistory();
spy(5);

wrapper.setProps({ id: 2 });
spy(5);
expect(spy.args).to.deep.equal([
[5],
[5],
]);
expectArgs(spy, 5, []);
});

class Test extends PureComponent {
Expand Down Expand Up @@ -312,8 +288,8 @@ export default function describeCDU({

describe('Own PureComponent implementation', () => {
it('does not update when state and props did not change', () => {
const spy = sinon.spy();
spy(1);
const spy = argSpy();

class Foo extends React.Component {
constructor(props) {
super(props);
Expand All @@ -339,55 +315,32 @@ export default function describeCDU({
}

const wrapper = Wrap(<Foo id={1} />);
spy(1);
expect(spy.args).to.deep.equal([
[1],
expectArgs(spy, 1, [
['render'],
[1],
]);
spy.resetHistory();
spy(2);

wrapper.setState({ foo: 'update' });
spy(2);
expect(spy.args).to.deep.equal([
[2],
expectArgs(spy, 2, [
['shouldComponentUpdate'],
['render'],
['componentDidUpdate'],
[2],
]);
spy.resetHistory();
spy(3);

wrapper.setState({ foo: 'update' });
spy(3);
expect(spy.args).to.deep.equal([
[3],
expectArgs(spy, 3, [
['shouldComponentUpdate'],
[3],
]);
spy.resetHistory();
spy(4);

wrapper.setProps({ id: 2 });
spy(4);
expect(spy.args).to.deep.equal([
[4],
expectArgs(spy, 4, [
['shouldComponentUpdate'],
['render'],
['componentDidUpdate'],
[4],
]);
spy.resetHistory();
spy(5);

wrapper.setProps({ id: 2 });
spy(5);
expect(spy.args).to.deep.equal([
[5],
expectArgs(spy, 5, [
['shouldComponentUpdate'],
[5],
]);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import sinon from 'sinon-sandbox';
import { expect } from 'chai';

import {
describeIf,
argSpy,
expectArgs,
} from '../../_helpers';
import {
is,
Expand All @@ -14,8 +14,7 @@ export default function describeGSBU({
}) {
describeIf(is('>= 16.3'), 'getSnapshotBeforeUpdate()', () => {
it('calls getSnapshotBeforeUpdate and pass snapshot to componentDidUpdate', () => {
const spy = sinon.spy();
spy(1);
const spy = argSpy();

class Foo extends React.Component {
constructor(props) {
Expand All @@ -40,35 +39,22 @@ export default function describeGSBU({
}
}
const wrapper = Wrap(<Foo name="foo" />);
spy(1);
expect(spy.args).to.deep.equal([
[1],
expectArgs(spy, 1, [
['render'],
[1],
]);
spy.resetHistory();
spy(2);

wrapper.setProps({ name: 'bar' });
spy(2);
expect(spy.args).to.deep.equal([
[2],
expectArgs(spy, 2, [
['render'],
['getSnapshotBeforeUpdate', { name: 'foo' }, { name: 'bar' }, { foo: 'bar' }, { foo: 'bar' }],
['componentDidUpdate', { name: 'foo' }, { name: 'bar' }, { foo: 'bar' }, { foo: 'bar' }, { snapshot: 'ok' }],
[2],
]);
spy.resetHistory();
spy(3);

wrapper.setState({ foo: 'baz' });
spy(3);
expect(spy.args).to.deep.equal([
[3],
expectArgs(spy, 3, [
['render'],
['getSnapshotBeforeUpdate', { name: 'bar' }, { name: 'bar' }, { foo: 'bar' }, { foo: 'baz' }],
['componentDidUpdate', { name: 'bar' }, { name: 'bar' }, { foo: 'bar' }, { foo: 'baz' }, { snapshot: 'ok' }],
[3],
]);
});
});
Expand Down

0 comments on commit 9c92f91

Please sign in to comment.