-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a complementary concat function for wrappers
- Loading branch information
1 parent
3bc6fda
commit 719a059
Showing
4 changed files
with
148 additions
and
92 deletions.
There are no files selected for viewing
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,132 +1,173 @@ | ||
import { MemoryRouter, Route, Routes } from 'react-router-dom'; | ||
import { QueryClientProvider, QueryClient, useQuery } from '@tanstack/react-query'; | ||
import { wrap } from '../wrap'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
describe('fn(wrap)', () => { | ||
it('should support not providing props', async () => { | ||
const Component = wrap(MemoryRouter); | ||
describe('utils(wrap)', () => { | ||
describe('fn(wrap)', () => { | ||
it('should support not providing props', async () => { | ||
const Component = wrap(MemoryRouter); | ||
|
||
const MyComponent = await Component(() => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
</Routes> | ||
)); | ||
|
||
render(<MyComponent />); | ||
const MyComponent = await Component(() => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
</Routes> | ||
)); | ||
|
||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
render(<MyComponent />); | ||
|
||
it('should support options as direct props', async () => { | ||
const Component = wrap(MemoryRouter, { | ||
initialEntries: ['/profile'], | ||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
|
||
const MyComponent = await Component(() => ( | ||
<Routes> | ||
<Route path="/profile" element={'Profile'} /> | ||
</Routes> | ||
)); | ||
|
||
render(<MyComponent />); | ||
|
||
expect(screen.getByText('Profile')).toBeTruthy(); | ||
}); | ||
|
||
it('should support options as a function that returns the props', async () => { | ||
const Component = wrap(MemoryRouter, () => ({ | ||
initialEntries: ['/profile'], | ||
})); | ||
it('should support options as direct props', async () => { | ||
const Component = wrap(MemoryRouter, { | ||
initialEntries: ['/profile'], | ||
}); | ||
|
||
const MyComponent = await Component(() => ( | ||
<Routes> | ||
<Route path="/profile" element={'Profile'} /> | ||
</Routes> | ||
)); | ||
const MyComponent = await Component(() => ( | ||
<Routes> | ||
<Route path="/profile" element={'Profile'} /> | ||
</Routes> | ||
)); | ||
|
||
render(<MyComponent />); | ||
render(<MyComponent />); | ||
|
||
expect(screen.getByText('Profile')).toBeTruthy(); | ||
}); | ||
expect(screen.getByText('Profile')).toBeTruthy(); | ||
}); | ||
|
||
it('should providing components via promises', async () => { | ||
const Component = wrap(MemoryRouter); | ||
it('should support options as a function that returns the props', async () => { | ||
const Component = wrap(MemoryRouter, () => ({ | ||
initialEntries: ['/profile'], | ||
})); | ||
|
||
const MyComponent = await Component( | ||
Promise.resolve(() => ( | ||
const MyComponent = await Component(() => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
<Route path="/profile" element={'Profile'} /> | ||
</Routes> | ||
)) | ||
); | ||
)); | ||
|
||
render(<MyComponent />); | ||
render(<MyComponent />); | ||
|
||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
expect(screen.getByText('Profile')).toBeTruthy(); | ||
}); | ||
|
||
it('should providing components via react routers async Component format', async () => { | ||
const Component = wrap(MemoryRouter); | ||
it('should providing components via promises', async () => { | ||
const Component = wrap(MemoryRouter); | ||
|
||
const MyComponent = await Component( | ||
Promise.resolve({ | ||
Component: () => ( | ||
const MyComponent = await Component( | ||
Promise.resolve(() => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
</Routes> | ||
), | ||
}) | ||
); | ||
)) | ||
); | ||
|
||
render(<MyComponent />); | ||
render(<MyComponent />); | ||
|
||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
|
||
it('should providing components via default async exports', async () => { | ||
const Component = await wrap(MemoryRouter); | ||
it('should providing components via react routers async Component format', async () => { | ||
const Component = wrap(MemoryRouter); | ||
|
||
const MyComponent = await Component( | ||
Promise.resolve({ | ||
default: () => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
</Routes> | ||
), | ||
}) | ||
); | ||
const MyComponent = await Component( | ||
Promise.resolve({ | ||
Component: () => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
</Routes> | ||
), | ||
}) | ||
); | ||
|
||
render(<MyComponent />); | ||
render(<MyComponent />); | ||
|
||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
|
||
it('should providing components via default async exports', async () => { | ||
const mockOptions = jest.fn(); | ||
const Component = await wrap(MemoryRouter, mockOptions); | ||
it('should providing components via default async exports', async () => { | ||
const Component = wrap(MemoryRouter); | ||
|
||
const MyComponent = await Component( | ||
Promise.resolve({ | ||
default: () => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
</Routes> | ||
), | ||
}) | ||
); | ||
const MyComponent = await Component( | ||
Promise.resolve({ | ||
default: () => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
</Routes> | ||
), | ||
}) | ||
); | ||
|
||
expect(mockOptions).toHaveBeenCalledTimes(0); | ||
render(<MyComponent />); | ||
|
||
render(<MyComponent />); | ||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
|
||
expect(mockOptions).toHaveBeenCalledTimes(1); | ||
it('should providing components via default async exports', async () => { | ||
const mockOptions = jest.fn(); | ||
const Component = wrap(MemoryRouter, mockOptions); | ||
|
||
const { rerender } = render(<MyComponent />); | ||
const MyComponent = await Component( | ||
Promise.resolve({ | ||
default: () => ( | ||
<Routes> | ||
<Route index element={'Index'} /> | ||
</Routes> | ||
), | ||
}) | ||
); | ||
|
||
expect(mockOptions).toHaveBeenCalledTimes(2); | ||
expect(mockOptions).toHaveBeenCalledTimes(0); | ||
|
||
rerender(<MyComponent />); | ||
render(<MyComponent />); | ||
|
||
expect(mockOptions).toHaveBeenCalledTimes(2); | ||
expect(mockOptions).toHaveBeenCalledTimes(1); | ||
|
||
const { rerender } = render(<MyComponent />); | ||
|
||
expect(mockOptions).toHaveBeenCalledTimes(2); | ||
|
||
rerender(<MyComponent />); | ||
|
||
expect(mockOptions).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
|
||
describe('fn(wrap.concat)', () => { | ||
it('should support combining wrappers', async () => { | ||
const Router = wrap(MemoryRouter); | ||
const ReactQuery = wrap(QueryClientProvider, () => ({ | ||
client: new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}), | ||
})); | ||
|
||
const KitchenSink = wrap.concat(Router, ReactQuery); | ||
|
||
const MyComponent = await KitchenSink(({ value }: { value: string }) => { | ||
const { data, isSuccess } = useQuery({ | ||
queryKey: [], | ||
queryFn: async () => 'hello world!', | ||
}); | ||
|
||
return ( | ||
<> | ||
{isSuccess && <div data-testid="query">{data}</div>} | ||
<Routes> | ||
<Route index element={value} /> | ||
</Routes> | ||
</> | ||
); | ||
}); | ||
|
||
render(<MyComponent value={'Index'} />); | ||
|
||
expect(screen.findByTestId('query')).toBeTruthy(); | ||
expect(screen.getByText('Index')).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
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