Skip to content

Commit

Permalink
tests: use range in drawer tests (katspaugh#2047)
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Aug 25, 2020
1 parent 1dd995a commit 2784a84
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
'no-extra-semi': "error",
'no-multi-spaces': "error",
'no-multiple-empty-lines': "error",
'space-infix-ops': "error",
'valid-jsdoc': [2, {
'requireReturn': false,
'requireReturnType': false
Expand Down
16 changes: 8 additions & 8 deletions spec/drawer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ describe('Drawer', function() {
it('handleEvent should return 0.5 if clicked in the middle of wrapper', function() {
const {right, width} = drawer.wrapper.getBoundingClientRect();

expect(drawer.handleEvent({clientX: right - width/2}, true)).toBeCloseTo(0.5, 4); // because 0.1 + 0.2 !== 0.3
expect(drawer.handleEvent({clientX: right - width / 2}, true)).toBeWithinRange(0.49, 0.51); // because 0.1 + 0.2 !== 0.3
});

/** @test {handleEvent/0.9} */
it('handleEvent should return 0.9 if clicked 10% from the end', function() {
const {right, width} = drawer.wrapper.getBoundingClientRect();

expect(drawer.handleEvent({clientX: right - width/10}, true)).toBeCloseTo(0.9, 4); // because 0.1 + 0.2 !== 0.3
expect(drawer.handleEvent({clientX: right - width / 10}, true)).toBeWithinRange(0.89, 0.91); // because 0.1 + 0.2 !== 0.3
});

/** @test {handleEvent/left} */
Expand All @@ -49,15 +49,15 @@ describe('Drawer', function() {
it('handleEvent should return 0 if clicked on wrapper left position -1px', function() {
const {left} = drawer.wrapper.getBoundingClientRect();

expect(drawer.handleEvent({clientX: left-1}, true)).toBe(0);
expect(drawer.handleEvent({clientX: left - 1}, true)).toBe(0);
});

/** @test {handleEvent/left+1} */
it('handleEvent should not return 0 if clicked on wrapper left position +1px', function() {
const {left} = drawer.wrapper.getBoundingClientRect();

expect(drawer.handleEvent({clientX: left+1}, true)).not.toBe(0);
expect(drawer.handleEvent({clientX: left+1}, true)).toBeGreaterThan(0);
expect(drawer.handleEvent({clientX: left + 1}, true)).not.toBe(0);
expect(drawer.handleEvent({clientX: left + 1}, true)).toBeGreaterThan(0);
});

/** @test {handleEvent/right} */
Expand All @@ -71,15 +71,15 @@ describe('Drawer', function() {
it('handleEvent should return 1 if clicked on wrapper right position +1px', function() {
const {right} = drawer.wrapper.getBoundingClientRect();

expect(drawer.handleEvent({clientX: right+1}, true)).toBe(1);
expect(drawer.handleEvent({clientX: right + 1}, true)).toBe(1);
});

/** @test {handleEvent/right-1} */
it('handleEvent should not return 1 if clicked on wrapper right position -1px', function() {
const {right} = drawer.wrapper.getBoundingClientRect();

expect(drawer.handleEvent({clientX: right-1}, true)).not.toBe(1);
expect(drawer.handleEvent({clientX: right-1}, true)).toBeLessThan(1);
expect(drawer.handleEvent({clientX: right - 1}, true)).not.toBe(1);
expect(drawer.handleEvent({clientX: right - 1}, true)).toBeLessThan(1);
});

});

0 comments on commit 2784a84

Please sign in to comment.