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

fix: functions were modifying input options object #65

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
13 changes: 5 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ const getToleranceFromOpts = (opts) => {
};

const prepareOpts = (opts) => {
opts.tolerance = getToleranceFromOpts(opts);
const tolerance = getToleranceFromOpts(opts);

return _.defaults(opts, {
return _.defaults({}, opts, {
ignoreCaret: true,
ignoreAntialiasing: true,
antialiasingTolerance: 0
antialiasingTolerance: 0,
tolerance: tolerance
});
};

Expand Down Expand Up @@ -228,11 +229,7 @@ exports.createDiff = function saveDiff(opts, callback) {
};

exports.colors = (color1, color2, opts) => {
opts = opts || {};

if (opts.tolerance === undefined) {
opts.tolerance = JND;
}
opts = _.defaults({}, opts, {tolerance: JND});

const comparator = makeCIEDE2000Comparator(opts.tolerance);

Expand Down
38 changes: 38 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ describe('looksSame', () => {
});
});

it('should not modify input options', (done) => {
const opts = {};

looksSame(srcPath('ref.png'), srcPath('same.png'), opts, () => {
expect(Object.keys(opts)).to.deep.equal([]);
done();
});
});

forFilesAndBuffers((getImage) => {
it('should return true for similar images', (done) => {
looksSame(getImage('ref.png'), getImage('same.png'), (error, {equal}) => {
Expand Down Expand Up @@ -398,6 +407,24 @@ describe('createDiff', () => {
}).to.throw(TypeError);
});

it('should not modify input options', (done) => {
const opts = {
reference: srcPath('ref.png'),
current: srcPath('same.png'),
diff: this.tempName,
highlightColor: '#ff00ff'
};

looksSame.createDiff(opts, () => {
expect(Object.keys(opts)).to.deep.equal(
['reference', 'current', 'diff', 'highlightColor']
);
done();
});

// If we got here, the call didn't attempt to modify opts
});

it('should format images', (done) => {
sandbox.spy(utils, 'formatImages');

Expand Down Expand Up @@ -762,6 +789,17 @@ describe('createDiff', () => {
});

describe('colors', () => {
it('should not modify input options', () => {
const opts = {};

looksSame.colors(
{R: 255, G: 0, B: 0},
{R: 255, G: 0, B: 0},
opts);

expect(Object.keys(opts)).to.deep.equal([]);
});

it('should return true for same colors', () => {
expect(
looksSame.colors(
Expand Down