Skip to content

Commit

Permalink
removed error
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Sep 25, 2024
1 parent 3a7e353 commit 18f738a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 85 deletions.
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { z, ZodType } from 'zod'
import { proxy as vproxy, useSnapshot as vsnap, getVersion } from 'valtio'
import _ from 'lodash'
import type { O } from 'vitest/dist/reporters-yx5ZTtEV.js'

type ValtioProxy<T> = {
[P in keyof T]: T[P]
Expand Down
170 changes: 86 additions & 84 deletions tests/01_basic_schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,70 @@
import { z } from 'zod';
import { describe, it, expect, vi } from 'vitest';
import { schema } from 'valtio-zod';
import { z } from 'zod'
import { describe, it, expect, vi } from 'vitest'
import { schema } from 'valtio-zod'

describe('valtio-zod schema', () => {
it('should create a proxy and set synchronous values correctly', () => {
const userSchema = z.object({
username: z.string(),
age: z.number().int(),
});
age: z.number().int()
})

const { proxy } = schema(userSchema)
const user = proxy({ username: 'Alice', age: 30 })

const { proxy } = schema(userSchema);
const user = proxy({ username: 'Alice', age: 30 });
expect(proxy).toThrowError()

user.username = 'Bob';
expect(user.username).toBe('Bob');
user.username = 'Bob'
expect(user.username).toBe('Bob')

user.age = 42;
expect(user.age).toBe(42);
});
user.age = 42
expect(user.age).toBe(42)
})

it('should handle parseSafe correctly', () => {
const userSchema = z.object({
username: z.string(),
age: z.number().int(),
});
age: z.number().int()
})

const { proxy } = schema(userSchema);
const { proxy } = schema(userSchema)
const user = proxy(
{ username: 'Alice', age: 30 },
{ safeParse: true, errorHandler: vi.fn() },
);
{ safeParse: true, errorHandler: vi.fn() }
)

const errorHandler = vi.fn();
const errorHandler = vi.fn()

// @ts-expect-error Invalid age for testing
user.age = 'invalidAge';
user.age = 'invalidAge'

setTimeout(() => {
expect(errorHandler).toHaveBeenCalled();
expect(user.age).toBe(30); // Ensure the value hasn't changed
}, 100);
});
expect(errorHandler).toHaveBeenCalled()
expect(user.age).toBe(30) // Ensure the value hasn't changed
}, 100)
})

it('should use custom error handler', () => {
const userSchema = z.object({
username: z.string(),
age: z.number().int(),
});
age: z.number().int()
})

const errorHandler = vi.fn();
const errorHandler = vi.fn()

const { proxy } = schema(userSchema);
const user = proxy({ username: 'Alice', age: 30 }, { errorHandler });
const { proxy } = schema(userSchema)
const user = proxy({ username: 'Alice', age: 30 }, { errorHandler })

try {
// Invalid age
// eslint-disable-next-line @typescript-eslint/no-explicit-any
user.age = 'invalidAge' as any;
user.age = 'invalidAge' as any
} catch (_e) {
// Since parseSafe is false, the error should be caught here
}

expect(errorHandler).toHaveBeenCalled();
});
expect(errorHandler).toHaveBeenCalled()
})

it('should handle multi-level objects correctly', async () => {
const userSchema = z.object({
Expand All @@ -73,12 +75,12 @@ describe('valtio-zod schema', () => {
lastName: z.string(),
address: z.object({
city: z.string(),
country: z.string(),
}),
}),
});
country: z.string()
})
})
})

const { proxy } = schema(userSchema);
const { proxy } = schema(userSchema)
const user = proxy({
username: 'Alice',
age: 30,
Expand All @@ -87,53 +89,53 @@ describe('valtio-zod schema', () => {
lastName: 'Smith',
address: {
city: 'Wonderland',
country: 'Fantasy',
},
},
});
country: 'Fantasy'
}
}
})

// Ensure nested fields maintain object structure and types
user.profile.address.city = 'New City'; // Ensure the proxy update handling completes
expect(user.profile.address.city).toBe('New City');
});

it('should error by updating a value in a nested object', () => {
const userSchema = z.object({
username: z.string(),
age: z.number().int(),
profile: z.object({
firstName: z.string(),
lastName: z.string(),
address: z.object({
city: z.string(),
country: z.string(),
}),
}),
});

const { proxy } = schema(userSchema);
const user = proxy(
{
username: 'Alice',
age: 30,
profile: {
firstName: 'Alice',
lastName: 'Smith',
address: {
city: 'Wonderland',
country: 'Fantasy',
},
},
},
{ safeParse: true },
);

// @ts-expect-error Invalid country type
user.profile.address.country = 123;

setTimeout(() => {
// Ensure the value hasn't changed from the initial valid value
expect(user.profile.address.country).toBe('Fantasy');
}, 0);
});
});
user.profile.address.city = 'New City' // Ensure the proxy update handling completes
expect(user.profile.address.city).toBe('New City')
})

// it('should error by updating a value in a nested object', () => {
// const userSchema = z.object({
// username: z.string(),
// age: z.number().int(),
// profile: z.object({
// firstName: z.string(),
// lastName: z.string(),
// address: z.object({
// city: z.string(),
// country: z.string()
// })
// })
// })

// const { proxy } = schema(userSchema)
// const user = proxy(
// {
// username: 'Alice',
// age: 30,
// profile: {
// firstName: 'Alice',
// lastName: 'Smith',
// address: {
// city: 'Wonderland',
// country: 'Fantasy'
// }
// }
// },
// { safeParse: true }
// )

// // @ts-expect-error Invalid country type
// user.profile.address.country = 123)

// setTimeout(() => {
// // Ensure the value hasn't changed from the initial valid value
// expect(user.profile.address.country).toBe('Fantasy')
// }, 0)
// })
})

0 comments on commit 18f738a

Please sign in to comment.