Skip to content

Commit

Permalink
test: Added test code for the hasValueInReadOnlyStringList and hasPro…
Browse files Browse the repository at this point in the history
…perty utility functions (#43)
  • Loading branch information
ssi02014 committed Apr 15, 2024
1 parent d998d29 commit 07c7ff2
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { getFirstConsonants, hasBatchim } from './utils';
import { describe, expect, expectTypeOf, it } from 'vitest';
import { getFirstConsonants, hasBatchim, hasProperty, hasValueInReadOnlyStringList } from './utils';

describe('hasBatchim', () => {
it('should return true for the character "값"', () => {
Expand Down Expand Up @@ -37,3 +37,55 @@ describe('getFirstConsonants', () => {
expect(getFirstConsonants('띄어 쓰기')).toBe('ㄸㅇ ㅆㄱ');
});
});

describe('hasValueInReadOnlyStringList', () => {
const testReadonlyList = ['ㄱ', 'ㄴ', 'ㄷ'] as const;

it('should return true if an element exists in a read-only string list', () => {
const testValue = 'ㄱ';

expect(hasValueInReadOnlyStringList(testReadonlyList, testValue)).toBeTruthy();
});

it('should return false if an element does not exist in a read-only string list', () => {
const testValue = 'ㄹ';

expect(hasValueInReadOnlyStringList(testReadonlyList, testValue)).toBeFalsy();
});

it('should narrow the type of the second argument if it is included in a read-only string list', () => {
const testValue = 'ㄱ' as string;

if (hasValueInReadOnlyStringList(testReadonlyList, testValue)) {
expectTypeOf(testValue).toEqualTypeOf<'ㄱ' | 'ㄴ' | 'ㄷ'>();
} else {
expectTypeOf(testValue).toEqualTypeOf<string>();
}
});
});

describe('hasProperty', () => {
const testObj = { : 'ㄱ', : 'ㄴ', : 'ㄷ' } as const;

it('should return true if a property exists in a object', () => {
const testKey = 'ㄱ';

expect(hasProperty(testObj, testKey)).toBeTruthy();
});

it('should return false if a property does not exist in a object', () => {
const testKey = 'ㄹ';

expect(hasProperty(testObj, testKey)).toBeFalsy();
});

it('should narrow the type of the second argument if it is included in a object', () => {
const testKey = 'ㄱ' as string;

if (hasProperty(testObj, testKey)) {
expectTypeOf(testKey).toEqualTypeOf<'ㄱ' | 'ㄴ' | 'ㄷ'>();
} else {
expectTypeOf(testKey).toEqualTypeOf<string>();
}
});
});

0 comments on commit 07c7ff2

Please sign in to comment.