Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with future jasmine versions #367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/jasmine-enzyme/spec/customEqualityTesters--spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable global-require */
'use strict'; // eslint-disable-line

const { shallow } = require('enzyme');
const React = require('react');
const jasmineEnzyme = require('../lib/index.js');

describe('Support for Jasmine custom equality testers', () => {
function createMatcherFactory() {
spyOn(jasmine, 'addMatchers');
jasmineEnzyme();
const addMatchersArgs = jasmine.addMatchers.calls
.allArgs()
.filter(args => args[0].toHaveState)[0];
return addMatchersArgs[0].toHaveState;
}

it('works with Jasmine <3.6', () => {
const matcherFactory = createMatcherFactory();
const Fixture = require('./fixtures/toHaveState.fixture');
const wrapper = shallow(React.createElement(Fixture));
const util = {
equals: (a, b, testers) => testers[0](a, b),
};
const customEqualityTesters = [(a, b) => a.toString() === b.toString()];
const matcher = matcherFactory(util, customEqualityTesters);

const result = matcher.compare(wrapper, 'foo', 'false');
expect(result.pass).toEqual(true);
});

it('works with Jasmine >= 3.6 without triggering deprecation warnings', () => {
const matcherFactory = createMatcherFactory();
const Fixture = require('./fixtures/toHaveState.fixture');
const wrapper = shallow(React.createElement(Fixture));
const customEqualityTesters = [(a, b) => a.toString() === b.toString()];
customEqualityTesters.deprecated = true;
const util = jasmine.createSpyObj('matchersUtil', ['equals']);
util.equals.and.callFake((a, b) => customEqualityTesters[0](a, b));
const matcher = matcherFactory(util, customEqualityTesters);

const result = matcher.compare(wrapper, 'foo', 'false');
expect(result.pass).toEqual(true);
expect(util.equals).toHaveBeenCalled();

// Make sure we don't trigger any deprecation warnings
expect(matcherFactory.length).toBeLessThan(2);
expect(util.equals).not.toHaveBeenCalledWith(
jasmine.anything(),
jasmine.anything(),
customEqualityTesters
);
});

it('works if Jasmine does not pass customEqualityTesters', () => {
const matcherFactory = createMatcherFactory();
const Fixture = require('./fixtures/toHaveState.fixture');
const wrapper = shallow(React.createElement(Fixture));
const util = {
equals: () => true,
};
const matcher = matcherFactory(util);

const result = matcher.compare(wrapper, 'foo', 'false');
expect(result.pass).toEqual(true);
});
});
5 changes: 4 additions & 1 deletion packages/jasmine-enzyme/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ function jasmineEnzyme(): void {

const toJasmineMatcher = (matcherFn: Function) => (
util: Object,
customEqualityTesters: Object
...extraArgs
) => {
const customEqualityTesters =
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed if compatibility with jasmine 3.5 and earlier is not wanted.

extraArgs[0] && !extraArgs[0].deprecated ? extraArgs[0] : undefined;

// Convert the equals util from jasmine to share the same interface as jest
const equals = (actual, expected) =>
util.equals(actual, expected, customEqualityTesters);
Expand Down