-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
182 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,93 @@ | ||
import { setupServer } from 'msw/node' | ||
import { http } from 'msw' | ||
import { router } from '@inertiajs/core' | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
import { server } from './server.mock' | ||
import { useInertiaForm } from '../src' | ||
|
||
const server = setupServer( | ||
http.post('/form', async () => {}) | ||
) | ||
|
||
beforeAll(() => server.listen()) | ||
afterEach(() => server.resetHandlers()) | ||
afterAll(() => server.close()) | ||
|
||
describe('submit', () => { | ||
it('should handle response details correctly', async () => { | ||
const testData = { | ||
email: 'some name', | ||
} | ||
|
||
jest.spyOn(router, 'post').mockImplementation((url, data, options) => { | ||
options.onError({ | ||
// @ts-ignore | ||
describe('flat data forms with errors', () => { | ||
it('should handle flat data with one key', async () => { | ||
const testData = { | ||
email: 'some name', | ||
} | ||
|
||
jest.spyOn(router, 'post').mockImplementation((url, data, options) => { | ||
options.onError({ | ||
// @ts-ignore | ||
email: ['must exist'], | ||
}) | ||
}) | ||
|
||
const { result } = renderHook(() => useInertiaForm(testData)) | ||
|
||
await act(async () => { | ||
result.current.submit('post', '/api/data-error') | ||
}) | ||
|
||
expect(result.current.errors).toMatchObject({ | ||
email: ['must exist'], | ||
}) | ||
}) | ||
|
||
const { result } = renderHook(() => useInertiaForm(testData)) | ||
it('should flat data with more than one key', async () => { | ||
const testData = { | ||
email: 'some email', | ||
user: 'some name', | ||
} | ||
|
||
jest.spyOn(router, 'post').mockImplementation((url, data, options) => { | ||
options.onError({ | ||
// @ts-ignore | ||
email: ['must exist'], | ||
// @ts-ignore | ||
username: ['must exist'], | ||
}) | ||
}) | ||
|
||
const { result } = renderHook(() => useInertiaForm(testData)) | ||
|
||
await act(async () => { | ||
result.current.submit('post', '/api/data-error') | ||
}) | ||
|
||
await act(async () => { | ||
result.current.submit('post', '/form') | ||
expect(result.current.errors).toMatchObject({ | ||
email: ['must exist'], | ||
username: ['must exist'], | ||
}) | ||
}) | ||
}) | ||
|
||
describe('nested data', () => { | ||
it('should nest errors', async () => { | ||
const testData = { | ||
user: { | ||
email: 'some email', | ||
username: 'some name', | ||
}, | ||
} | ||
|
||
expect(result.current.errors).toMatchObject({ | ||
email: ['must exist'], | ||
jest.spyOn(router, 'post').mockImplementation((url, data, options) => { | ||
options.onError({ | ||
// @ts-ignore | ||
email: ['must exist'], | ||
// @ts-ignore | ||
username: ['must exist'], | ||
}) | ||
}) | ||
|
||
const { result } = renderHook(() => useInertiaForm(testData)) | ||
|
||
await act(async () => { | ||
result.current.submit('post', '/api/data-error') | ||
}) | ||
|
||
expect(result.current.errors).toMatchObject({ | ||
'user.email': ['must exist'], | ||
'user.username': ['must exist'], | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { isUnset } from "../../src/utils" | ||
|
||
describe('isUnset', () => { | ||
describe('numbers', () => { | ||
it('should be false for any number', () => { | ||
expect(isUnset(0)).toBe(false) | ||
expect(isUnset(-0)).toBe(false) | ||
expect(isUnset(1)).toBe(false) | ||
expect(isUnset(67959516)).toBe(false) | ||
expect(isUnset(-66)).toBe(false) | ||
expect(isUnset(-985619874)).toBe(false) | ||
}) | ||
|
||
it('should be true for NaN', () => { | ||
expect(isUnset(NaN)).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('strings', () => { | ||
it('should be false for any non-empty string', () => { | ||
expect(isUnset('string')).toBe(false) | ||
expect(isUnset(' ')).toBe(false) | ||
expect(isUnset(`string literal ${1 + 1}`)).toBe(false) | ||
}) | ||
|
||
it('should be true for empty strings', () => { | ||
expect(isUnset('')).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('dates', () => { | ||
it('should be false for any valid date object', () => { | ||
expect(isUnset(new Date())).toBe(false) | ||
}) | ||
|
||
it('should be true for any invalid date object', () => { | ||
expect(isUnset(new Date('not a date'))).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('booleans', () => { | ||
it('should be false for true and false', () => { | ||
expect(isUnset(true)).toBe(false) | ||
expect(isUnset(false)).toBe(false) | ||
}) | ||
|
||
it('should be true if variable is uninitialized', () => { | ||
let b: boolean | ||
expect(isUnset(b)).toBe(true) | ||
}) | ||
}) | ||
|
||
it('should be true for any "empty" primitive or empty object', () => { | ||
expect(isUnset(undefined)).toBe(true) | ||
expect(isUnset(null)).toBe(true) | ||
expect(isUnset([])).toBe(true) | ||
expect(isUnset({})).toBe(true) | ||
}) | ||
|
||
it('should be false for any non-empty value', () => { | ||
expect(isUnset({ key: "value" })).toBe(false) | ||
expect(isUnset(["one"])).toBe(false) | ||
expect(isUnset([1])).toBe(false) | ||
expect(isUnset([1, "two", { three: "four" }])).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters