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(formatters): format visibility percentage correctly #8

Open
wants to merge 1 commit into
base: alpha
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
Original file line number Diff line number Diff line change
Expand Up @@ -1051,19 +1051,19 @@ exports[`iOS description maker should process everything.jsonl fixture 1`] = `
+ {"id":"faceid","expected":"unavailable"}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"id","value":"halfVisible","isRegex":false},"expectation":"toBeVisible","params":[25],"timeout":2000}}
< Expect #halfVisible to be visible by 2500%
< Expect #halfVisible to be visible by 25%
+ {"id":"halfVisible","expected":25}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"id","value":"halfVisible","isRegex":false},"expectation":"toBeVisible","params":[50]}}
< Expect #halfVisible to be visible by 5000%
< Expect #halfVisible to be visible by 50%
+ {"id":"halfVisible","expected":50}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"id","value":"halfVisible","isRegex":false},"modifiers":["not"],"expectation":"toBeVisible","params":[26],"timeout":2000}}
< Expect #halfVisible not to be visible by 2600%
< Expect #halfVisible not to be visible by 26%
+ {"id":"halfVisible","expected":26}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"id","value":"halfVisible","isRegex":false},"modifiers":["not"],"expectation":"toBeVisible","params":[51]}}
< Expect #halfVisible not to be visible by 5100%
< Expect #halfVisible not to be visible by 51%
+ {"id":"halfVisible","expected":51}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"id","value":"localDateLabel","isRegex":false},"expectation":"toHaveText","params":["Date (Local): Feb 6th, 2019"]}}
Expand Down Expand Up @@ -1427,7 +1427,7 @@ exports[`iOS description maker should process everything.jsonl fixture 1`] = `
+ {"text":"From push"}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"text","value":"HText1","isRegex":false},"expectation":"toBeVisible","params":[1]}}
< Expect "HText1" to be visible by 100%
< Expect "HText1" to be visible by 1%
+ {"text":"HText1","expected":1}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"text","value":"HText1","isRegex":false},"expectation":"toBeVisible"}}
Expand Down Expand Up @@ -1583,7 +1583,7 @@ exports[`iOS description maker should process everything.jsonl fixture 1`] = `
+ {"text":"Tap Working!!!"}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"text","value":"Text1","isRegex":false},"expectation":"toBeVisible","params":[1]}}
< Expect "Text1" to be visible by 100%
< Expect "Text1" to be visible by 1%
+ {"text":"Text1","expected":1}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"text","value":"Text1","isRegex":false},"expectation":"toBeVisible"}}
Expand All @@ -1607,7 +1607,7 @@ exports[`iOS description maker should process everything.jsonl fixture 1`] = `
+ {"text":"Text12"}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"text","value":"Text16","isRegex":false},"modifiers":["not"],"expectation":"toBeVisible","params":[80]}}
< Expect "Text16" not to be visible by 8000%
< Expect "Text16" not to be visible by 80%
+ {"text":"Text16","expected":80}

> {"type":"invoke","params":{"type":"expectation","predicate":{"type":"text","value":"Text4","isRegex":false},"expectation":"toBeVisible"}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StepDescription } from '../../types';
import type { ExpectationInvocation, Invocation } from '../detox-payload';
import { formatPredicate } from './predicate-formatters';
import { concat, msg, percent, truncate } from './utils';
import { concat, msg, percentVisible, truncate } from './utils';

const formatExpectationVerb = (invocation: ExpectationInvocation): string => {
const hasNot = invocation.modifiers?.includes('not');
Expand All @@ -21,7 +21,9 @@ const formatExpectationParams = (invocation: ExpectationInvocation): StepDescrip

switch (invocation.expectation) {
case 'toBeVisible': {
return typeof expected === 'number' ? msg(`by ${percent(expected)}`, { expected }) : null;
return typeof expected === 'number'
? msg(`by ${percentVisible(expected)}`, { expected })
: null;
}

default: {
Expand Down
30 changes: 29 additions & 1 deletion src/steps/description-maker/ios/formatters/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { percent, truncate } from './utils';
import { percent, percentVisible, truncate } from './utils';

describe('percent', () => {
it('should return empty string for abnormal values', () => {
Expand Down Expand Up @@ -26,6 +26,34 @@ describe('percent', () => {
});
});

describe('percentVisible', () => {
it('should return empty string for abnormal values', () => {
expect(percentVisible('')).toBe('');
expect(percentVisible(null)).toBe('');
expect(percentVisible()).toBe('');
expect(percentVisible(Number.NaN)).toBe('');
expect(percentVisible(Number.POSITIVE_INFINITY)).toBe('');
expect(percentVisible([])).toBe('');
expect(percentVisible({})).toBe('');
expect(percentVisible('')).toBe('');
});

it('should convert valid numbers to strings', () => {
expect(percentVisible(75)).toBe('75%');
expect(percentVisible(1)).toBe('1%');
expect(percentVisible(100)).toBe('100%');
expect(percentVisible(0)).toBe('0%');
expect(percentVisible(33)).toBe('33%');
});

it('should handle string numbers', () => {
expect(percentVisible('50')).toBe('50%');
expect(percentVisible('0')).toBe('0%');
expect(percentVisible('100')).toBe('100%');
expect(percentVisible('1')).toBe('1%');
});
});

describe('truncate', () => {
it('should return empty string for falsy values', () => {
expect(truncate('')).toBe('');
Expand Down
12 changes: 12 additions & 0 deletions src/steps/description-maker/ios/formatters/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ export function percent(value?: unknown): string {
return Number.isFinite(num) ? (num * 100).toFixed(0) + '%' : '';
}

// Separate function for toBeVisible call as it's parameter is directly in %
export function percentVisible(value?: unknown): string {
const num =
typeof value === 'number'
? value
: typeof value === 'string' && value
? Number(value)
: Number.NaN;

return Number.isFinite(num) ? num.toFixed(0) + '%' : '';
}

export function truncate(value?: unknown, maxLength = 40): string {
if (!value) return '';

Expand Down