Skip to content

Commit

Permalink
tests(utils): add tests for boolean detection utils (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsvetkov-splunk committed Jul 18, 2024
1 parent 0d7a5d7 commit 6f7ac89
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
80 changes: 80 additions & 0 deletions ui/src/util/considerFalseAndTruthy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { isFalse, isTrue, getValueMapTruthyFalse } from './considerFalseAndTruthy';

describe('isFalse function', () => {
test.each([
[null, true],
[undefined, true],
['0', true],
['FALSE', true],
['F', true],
['N', true],
['NO', true],
['NONE', true],
['nOnE', true],
['', true],
[0, true],
[false, true],
['1', false],
['TRUE', false],
[1, false],
[true, false],
['random string', false],
[{}, false],
[[], true],
])('isFalse(%p) should return %p', (input, expected) => {
expect(isFalse(input)).toBe(expected);
});
});

describe('isTrue function', () => {
test.each([
[null, false],
[undefined, false],
['1', true],
['TRUE', true],
['tRuE', true],
['T', true],
['Y', true],
['YES', true],
[1, true],
[true, true],
['0', false],
['FALSE', false],
[0, false],
[false, false],
['random string', false],
[{}, false],
[[], false],
])('isTrue(%p) should return %p', (input, expected) => {
expect(isTrue(input)).toBe(expected);
});
});

describe('getValueMapTruthyFalse function', () => {
test.each([
[null, '0'],
[undefined, '0'],
['0', '0'],
['FALSE', '0'],
['F', '0'],
['N', '0'],
['NO', '0'],
['NONE', '0'],
['', '0'],
['1', '1'],
['TRUE', '1'],
['T', '1'],
['Y', '1'],
['YES', '1'],
['random string', 'random string'],
[1, '1'],
[0, '0'],
[123, 123],
[true, '1'],
[false, '0'],
[{}, {}],
[[], '0'],
])('getValueMapTruthyFalse(%p) should return %p', (input, expected) => {
expect(getValueMapTruthyFalse(input)).toEqual(expected);
});
});
4 changes: 2 additions & 2 deletions ui/src/util/considerFalseAndTruthy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export function isTrue(value: unknown) {
}

/**
* since splunk does not oeprate on booleans and numbers, but onyl strings
* here we use mapping to enable compering and oeprating on them
* since splunk does not operate on booleans and numbers, but only strings
* here we use mapping to enable comparing and operating on them
* @param value value used for mapping
* @returns maps truthy values into 1 and false into 0, does not modify rest
*/
Expand Down

0 comments on commit 6f7ac89

Please sign in to comment.