Skip to content

Commit

Permalink
add zod and scratchpad test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jul 12, 2024
1 parent 5911849 commit e2e30ed
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
11 changes: 10 additions & 1 deletion web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"lodash": "^4.17.21",
"lz-string": "^1.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react-dom": "^18.3.1",
"zod": "^3.23.8"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.4.5",
Expand Down
92 changes: 92 additions & 0 deletions web/test/lib_tests/scratchpad.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// https://stackoverflow.com/questions/78739104/how-can-i-avoid-duplicating-key-in-value-and-duplicating-type-and-const-definit
/* eslint-disable @typescript-eslint/no-unused-vars */
import _ from 'lodash'
import { describe, expect, test } from 'vitest'
import { z } from 'zod'

// type Name = 'foo' | 'bar'

// type ProviderFuncFromString = (data: string) => { name: Name; data: string }

// type ProviderFuncFromNumber = (numb: number) => { name: Name; data: string }

// function providerFuncFromStringFactory(name: Name): ProviderFuncFromString {
// return (data) => ({ name, data })
// }

// function providerFuncFromNumberFactory(name: Name): ProviderFuncFromNumber {
// return (numb) => ({ name, data: numb.toString() })
// }

// type ProviderMap = {
// foo: ProviderFuncFromString
// bar: ProviderFuncFromNumber
// }

// const providerMap: ProviderMap = {
// foo: providerFuncFromStringFactory('foo'),
// bar: providerFuncFromNumberFactory('bar'),
// }

const NameSchema = z.enum(['foo', 'bar'])

const ProviderFuncFromStringSchema = z
.function()
.args(z.string())
.returns(
z.object({
name: NameSchema,
data: z.string(),
}),
)

type ProviderFuncFromStringType = z.infer<typeof ProviderFuncFromStringSchema>

const ProviderFuncFromNumberSchema = z
.function()
.args(z.number())
.returns(
z.object({
name: NameSchema,
data: z.string(),
}),
)

type ProviderFuncFromNumberType = z.infer<typeof ProviderFuncFromNumberSchema>

function providerFuncFromStringFactory(
name: z.infer<typeof NameSchema>,
): ProviderFuncFromStringType {
return (data: string) => ({ name, data })
}

function providerFuncFromNumberFactory(
name: z.infer<typeof NameSchema>,
): ProviderFuncFromNumberType {
return (numb: number) => ({ name, data: numb.toString() })
}

const ProviderMapSchema = z.object({
foo: ProviderFuncFromStringSchema,
bar: ProviderFuncFromNumberSchema,
})

const providerMap1 = {
foo: providerFuncFromStringFactory('foo'),
bar: providerFuncFromNumberFactory('bar'),
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const providerMap2 = Object.fromEntries([
['foo', providerFuncFromNumberFactory('foo')],
['bar', providerFuncFromNumberFactory('bar')],
])

describe('scratchpad tests', () => {
test('test scratchpad', () => {
const parsedProviderMap1 = ProviderMapSchema.safeParse(providerMap1)
const parsedProviderMap2 = ProviderMapSchema.safeParse(providerMap2)
expect(parsedProviderMap1.success).toBe(true)
expect(parsedProviderMap2.success).toBe(false)

Check failure on line 90 in web/test/lib_tests/scratchpad.test.ts

View workflow job for this annotation

GitHub Actions / Test

test/lib_tests/scratchpad.test.ts > scratchpad tests > test scratchpad

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ test/lib_tests/scratchpad.test.ts:90:40

Check failure on line 90 in web/test/lib_tests/scratchpad.test.ts

View workflow job for this annotation

GitHub Actions / Test

test/lib_tests/scratchpad.test.ts > scratchpad tests > test scratchpad

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ test/lib_tests/scratchpad.test.ts:90:40
})
})

0 comments on commit e2e30ed

Please sign in to comment.