Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 3db2db6

Browse files
test: rewrites TextDiff unit tests
1 parent 09bd997 commit 3db2db6

File tree

3 files changed

+64
-104
lines changed

3 files changed

+64
-104
lines changed

lib/validators/text-diff.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class TextDiff {
2828

2929
validate() {
3030
this.valid = this.actual === this.expected;
31+
return this.valid;
3132
}
3233

3334
evaluateOutputToResults() {

test/cucumber/steps/general.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ module.exports = function() {
6969
});
7070

7171
this.Then('the validation result is:', function(expectedResult) {
72-
const dmp = new Diff();
7372
const stringifiedActual = JSON.stringify(this.result, null, 2);
7473

7574
expect(this.result).to.deep.equal(

test/unit/validators/text-diff-test.js

Lines changed: 63 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,107 @@
1-
/* eslint-disable */
2-
const { assert } = require('chai');
3-
4-
const fixtures = require('../../fixtures');
1+
/* eslint-disable no-new */
2+
const { expect } = require('chai');
53
const { TextDiff } = require('../../../lib/validators/text-diff');
6-
const {
7-
ValidationErrors
8-
} = require('../../../lib/validators/validation-errors');
94

105
describe('TextDiff', () => {
11-
validator = null;
12-
13-
describe('when i create new instance of validator with incorrect "data" (first argument)', () => {
14-
validator = null;
15-
16-
it('should throw exception', () => {
6+
describe('when expected non-string data', () => {
7+
it('should throw an exception', () => {
178
const fn = () => {
18-
validator = new TextDiff(null, '');
9+
new TextDiff(null, '');
1910
};
20-
assert.throws(fn);
11+
expect(fn).to.throw();
2112
});
2213
});
2314

24-
describe('when i create new instance of validator with incorrect "expected" (second argument)', () => {
25-
validator = null;
26-
27-
it('should throw exception', () => {
28-
fn = () => {
29-
validator = new TextDiff('', null);
15+
describe('when given non-string actual data', () => {
16+
it('should throw an exception', () => {
17+
const fn = () => {
18+
new TextDiff('', null);
3019
};
31-
assert.throws(fn);
20+
expect(fn).to.throw();
3221
});
3322
});
3423

35-
describe('when i create new instance of validator with "Iñtërnâtiônàlizætiøn☃" string as "data"', () => {
36-
validator = null;
24+
describe('when expected internationalized string', () => {
25+
const expected = 'Iñtërnâtiônàlizætiøn☃';
3726

38-
it('should not throw exception', () => {
39-
const fn = () => {
40-
validator = new TextDiff('Iñtërnâtiônàlizætiøn☃', '');
41-
};
42-
assert.doesNotThrow(fn);
27+
it('should resolve on matching actual string', () => {
28+
const validator = new TextDiff(expected, expected);
29+
expect(validator.validate()).to.be.true;
4330
});
4431

45-
describe('when I run validate', () => {
46-
it('should not throw exception', () => {
47-
const fn = () => validator.validate();
48-
assert.doesNotThrow(fn);
49-
});
32+
it('should reject on non-matching actual string', () => {
33+
const validator = new TextDiff(expected, 'Nâtiônàl');
34+
expect(validator.validate()).to.be.false;
5035
});
5136
});
5237

53-
describe('when i create new instance of validator with surrogate pair in data', () => {
54-
validator = null;
38+
describe('when expected a string with surrogate pair', () => {
39+
const expected = 'text1\uD800';
5540

56-
it('should not throw exception', () => {
57-
const fn = () => {
58-
validator = new TextDiff('text1\uD800', '\uD800text1');
59-
};
60-
assert.doesNotThrow(fn);
41+
it('should resolve on matching string', () => {
42+
const validator = new TextDiff(expected, 'text1\uD800');
43+
expect(validator.validate()).to.be.true;
6144
});
6245

63-
describe('when I run validate', () => {
64-
it('should not throw exception', () => {
65-
const fn = () => validator.validate();
66-
assert.doesNotThrow(fn);
67-
});
46+
it('should reject on non-matching string', () => {
47+
// This is not considered as non-matching strings
48+
// due to surrogate pairs in it. Rewrite the test.
49+
const validator = new TextDiff(expected, 'barry');
50+
expect(validator.validate()).to.be.false;
6851
});
6952
});
7053

71-
describe('when i create new instance of validator with correct data', () => {
72-
validator = null;
54+
describe('when expected textual data', () => {
55+
const expected = 'john';
7356

74-
it('should not throw exception', () => {
75-
const fn = () => {
76-
validator = new TextDiff('text1', 'text1');
77-
};
78-
assert.doesNotThrow(fn);
57+
it('should resolve when given matching actual data', () => {
58+
const validator = new TextDiff(expected, 'john');
59+
expect(validator.validate()).to.be.true;
7960
});
8061

81-
describe('when data are same and I run validate', () => {
82-
validationResult = null;
83-
84-
before(() => {
85-
validator = new TextDiff('text1', 'text1');
86-
validationResult = validator.validate();
87-
});
88-
89-
it('should set output property', () => {
90-
assert.isDefined(validator.valid);
91-
92-
it('output should be marked as valid', () => {
93-
assert.isTrue(validator.vaild);
94-
});
95-
});
62+
it('should reject when given non-matching actual data', () => {
63+
const validator = new TextDiff(expected, 'barry');
64+
expect(validator.validate()).to.be.false;
9665
});
66+
});
9767

98-
describe('when data differs and I run validate', () => {
99-
validationResult = null;
100-
101-
before(() => {
102-
validator = new TextDiff('text1', 'text2');
103-
validationResult = validator.validate();
104-
});
68+
describe('when evaluating output to results', () => {
69+
describe('when expected and actual data match', () => {
70+
const validator = new TextDiff('john', 'john');
71+
validator.validate();
72+
const result = validator.evaluateOutputToResults();
10573

106-
it('output property should not be marked as valid', () => {
107-
assert.isNotTrue(validator.valid);
74+
it('should return an empty array', () => {
75+
expect(result).to.be.instanceOf(Array);
76+
expect(result).to.have.lengthOf(0);
10877
});
10978
});
110-
});
11179

112-
describe('.evaluateOutputToResults', () => {
113-
data = null;
114-
results = null;
115-
116-
describe('empty validation result', () => {
117-
before(() => {
118-
validator = new TextDiff('', '');
119-
validator.validate();
120-
results = validator.evaluateOutputToResults();
121-
});
80+
describe('when expected and actual data do not match', () => {
81+
const validator = new TextDiff('john', 'barry');
82+
validator.validate();
83+
const result = validator.evaluateOutputToResults();
12284

12385
it('should return an array', () => {
124-
assert.isArray(results);
86+
expect(result).to.be.instanceOf(Array);
12587
});
12688

127-
it('should has no results', () => {
128-
assert.equal(results.length, 0);
89+
it('should contain exactly one error', () => {
90+
expect(result).to.have.lengthOf(1);
12991
});
130-
});
13192

132-
describe('non empty validation result', () => {
133-
before(() => {
134-
validator = new TextDiff('abc', 'cde');
135-
validator.validate();
136-
results = validator.evaluateOutputToResults();
93+
it('error should include the "message"', () => {
94+
expect(result[0]).to.have.property(
95+
'message',
96+
'Actual and expected data do not match.'
97+
);
13798
});
13899

139-
it('should return an array', () => {
140-
assert.isArray(results);
141-
});
142-
143-
it('should contain one error', () => {
144-
assert.lengthOf(results, 1);
100+
it('error should contain compared values', () => {
101+
expect(result[0]).to.have.deep.property('values', {
102+
expected: 'john',
103+
actual: 'barry'
104+
});
145105
});
146106
});
147107
});

0 commit comments

Comments
 (0)