Skip to content

Commit

Permalink
Improve D.fromPairs and D.toPairs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mobily committed Jul 30, 2022
1 parent 4c137a4 commit f72de16
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
15 changes: 10 additions & 5 deletions __tests__/Dict/fromPairs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ import { expectType } from 'ts-expect'

import { D, pipe } from '../..'

const spread = [] as readonly (readonly [string, string | number])[]

const xs = [
['day', 10],
['age', 20],
] as readonly (readonly [string, number])[]
] as const

const xsSpread = [...spread, ['day', 10], ['age', 20]] as const

const ys = [
const numbers = [
[0, 'Joe'],
[1, 20],
] as readonly (readonly [number, string | number])[]

describe('fromPairs', () => {
it('provides correct types', () => {
expectType<Record<string, number>>(D.fromPairs(xs))
expectType<Record<string, string | number>>(D.fromPairs(ys))
expectType<Record<string, 10 | 20>>(D.fromPairs(xs))
expectType<Record<string, string | number>>(D.fromPairs(xsSpread))
expectType<Record<string, string | number>>(D.fromPairs(numbers))
})

it('creates a new object from an array of tuples', () => {
Expand All @@ -30,7 +35,7 @@ describe('fromPairs', () => {
D.fromPairs([
['name', 'Joe'],
['location', 'Warsaw'],
]),
] as const),
).toEqual({ name: 'Joe', location: 'Warsaw' })
})
})
Expand Down
25 changes: 24 additions & 1 deletion __tests__/Dict/toPairs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectType } from 'ts-expect'

import { D, pipe } from '../..'
import { D, pipe, A } from '../..'

type T = {
readonly name: string
Expand All @@ -12,11 +12,25 @@ const user: T = {
age: 20,
}

const obj = {} as Record<string, number>
const number = {
0: 1,
1: 'ts-belt',
}
const symbol = {
[Symbol()]: 'oops',
}

describe('toPairs', () => {
it('provides correct types', () => {
expectType<ReadonlyArray<readonly [string, string | number]>>(
D.toPairs(user),
)
expectType<ReadonlyArray<readonly [string, number]>>(D.toPairs(obj))
expectType<ReadonlyArray<readonly [string, string | number]>>(
D.toPairs(number),
)
expectType<ReadonlyArray<readonly [string, string]>>(D.toPairs(symbol))
})

it('converts an object into an array of [key, value] tuples.', () => {
Expand Down Expand Up @@ -44,6 +58,15 @@ describe('toPairs (pipe)', () => {
expectType<ReadonlyArray<readonly [string, string | number]>>(
pipe(user, D.toPairs),
)

pipe(
user,
D.toPairs,
A.forEach(([key, value]) => {
expectType<string>(key)
expectType<string | number>(value)
}),
)
})

it('converts an object into an array of [key, value] tuples.', () => {
Expand Down

0 comments on commit f72de16

Please sign in to comment.