From 8e5fb4426186140c3c83a57001ac09dfad42a683 Mon Sep 17 00:00:00 2001 From: Asaf Korem Date: Wed, 13 Nov 2024 14:20:02 +0200 Subject: [PATCH] test(mapDeviceLongPressArguments): add unit tests. --- .../src/utils/mapDeviceLongPressArguments.js | 24 +++++++++---------- .../utils/mapDeviceLongPressArguments.test.js | 20 ++++++++++++++++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/detox/src/utils/mapDeviceLongPressArguments.js b/detox/src/utils/mapDeviceLongPressArguments.js index 69a60eba5c..045f3844df 100644 --- a/detox/src/utils/mapDeviceLongPressArguments.js +++ b/detox/src/utils/mapDeviceLongPressArguments.js @@ -6,7 +6,7 @@ function mapDeviceLongPressArguments(optionalAllParams, optionalDurationOrIgnore let point = null; let duration = null; let shouldIgnoreStatusBar = null; - + try { if (optionalAllParams === undefined) { // Do nothing. @@ -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; @@ -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 }; } diff --git a/detox/src/utils/mapDeviceLongPressArguments.test.js b/detox/src/utils/mapDeviceLongPressArguments.test.js index 2dc2f6033e..35118113ce 100644 --- a/detox/src/utils/mapDeviceLongPressArguments.test.js +++ b/detox/src/utils/mapDeviceLongPressArguments.test.js @@ -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(); + }); });