Skip to content

Commit

Permalink
test(mapDeviceLongPressArguments): add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
asafkorem committed Nov 13, 2024
1 parent 749ecf7 commit 8e5fb44
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
24 changes: 11 additions & 13 deletions detox/src/utils/mapDeviceLongPressArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function mapDeviceLongPressArguments(optionalAllParams, optionalDurationOrIgnore
let point = null;
let duration = null;
let shouldIgnoreStatusBar = null;

try {
if (optionalAllParams === undefined) {
// Do nothing.
Expand All @@ -25,23 +25,21 @@ function mapDeviceLongPressArguments(optionalAllParams, optionalDurationOrIgnore
} else {
assertPoint(optionalAllParams);
point = optionalAllParams;

if (typeof optionalDurationOrIgnoreStatusBar === 'number' || typeof optionalDurationOrIgnoreStatusBar === 'boolean') {
if (typeof optionalDurationOrIgnoreStatusBar === 'number') {
assertDuration(optionalDurationOrIgnoreStatusBar);
duration = optionalDurationOrIgnoreStatusBar;
} else {
assertShouldIgnoreStatusBar(optionalDurationOrIgnoreStatusBar);
shouldIgnoreStatusBar = optionalDurationOrIgnoreStatusBar;
assertUndefined(optionalIgnoreStatusBar);
}

if (typeof optionalDurationOrIgnoreStatusBar === 'number') {
assertDuration(optionalDurationOrIgnoreStatusBar);
duration = optionalDurationOrIgnoreStatusBar;
} else if (typeof optionalDurationOrIgnoreStatusBar === 'boolean') {
assertShouldIgnoreStatusBar(optionalDurationOrIgnoreStatusBar);
shouldIgnoreStatusBar = optionalDurationOrIgnoreStatusBar;
assertUndefined(optionalIgnoreStatusBar);
} else if (optionalDurationOrIgnoreStatusBar !== undefined) {
assertDuration(optionalDurationOrIgnoreStatusBar);
} else {
assertUndefined(optionalDurationOrIgnoreStatusBar);
assertUndefined(optionalIgnoreStatusBar);
}

if (optionalIgnoreStatusBar !== undefined) {
assertShouldIgnoreStatusBar(optionalIgnoreStatusBar);
shouldIgnoreStatusBar = optionalIgnoreStatusBar;
Expand All @@ -51,7 +49,7 @@ function mapDeviceLongPressArguments(optionalAllParams, optionalDurationOrIgnore
throw new DetoxRuntimeError(`longPress accepts either a duration (number) or a point ({x: number, y: number}) as ` +
`its first argument, optionally a duration (number) as its second argument, and optionally a ignoreStatusBar (boolean) as its third argument. Error: ${e.message}`);
}

return { point, duration, shouldIgnoreStatusBar };
}

Expand Down
20 changes: 20 additions & 0 deletions detox/src/utils/mapDeviceLongPressArguments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,24 @@ describe('mapDeviceLongPressArguments', () => {
it('should throw for invalid shouldIgnoreStatusBar', () => {
expect(() => mapDeviceLongPressArguments({ x: 1, y: 2 }, 3, 'true')).toThrowError('shouldIgnoreStatusBar should be a boolean, but got true (string)');
});

it('should return `{ point: { x: 1, y: 2 }, duration: null, shouldIgnoreStatusBar: true }` for `{ x: 1, y: 2 }, true`', () => {
expect(mapDeviceLongPressArguments({ x: 1, y: 2 }, true))
.toEqual({ point: { x: 1, y: 2 }, duration: null, shouldIgnoreStatusBar: true });
});

it('should return `{ point: { x: 1, y: 2 }, duration: null, shouldIgnoreStatusBar: false }` for `{ x: 1, y: 2 }, false`', () => {
expect(mapDeviceLongPressArguments({ x: 1, y: 2 }, false))
.toEqual({ point: { x: 1, y: 2 }, duration: null, shouldIgnoreStatusBar: false });
});

it('should throw when providing point, boolean, and extra argument', () => {
expect(() => mapDeviceLongPressArguments({ x: 1, y: 2 }, true, 'extra'))
.toThrowError();
});

it('should throw when providing duration, boolean, and extra argument', () => {
expect(() => mapDeviceLongPressArguments(1000, true, 'extra'))
.toThrowError();
});
});

0 comments on commit 8e5fb44

Please sign in to comment.