Files
modeling-app/src/components/UserSidebarMenu.test.tsx

137 lines
3.6 KiB
TypeScript
Raw Normal View History

import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import UserSidebarMenu from './UserSidebarMenu'
import {
Route,
RouterProvider,
createMemoryRouter,
createRoutesFromElements,
} from 'react-router-dom'
import { Models } from '@kittycad/lib'
File based settings (#1679) * Rename GlobalStateContext to SettingsAuthContext * Naive initial impl of settings persistence to file system * Update app identifier in tauri config * Add "show in folder" tauri command * Load from and save to file system in Tauri app * Add documents drive to tauri permission scope * Add recursive prop to default dir selection dialog * Add success toast to web restore defaults action * Add a way to validate read-in settings * Update imports to use separate settings lib file * Validate localStorage-loaded settings, combine error message * Add a e2e test for validation * Clean up state state bugs * Reverse validation looping so new users don't error * update settingsMachine typegen to remove conflicts * Fmt * Fix TS errors * Fix import paths, etc post-merge * Make default length units `mm` and 'metric' * Rename to SettingsAuth* * cargo fmt * Revert Tauri config identifier change * Update clientSideInfra's baseUnits from settings * Break apart CommandBar and CommandBarProvider * Bugfix: don't validate defaultValue when it's not configured * Allow some TauriFS functions to no-op from browser * Sidestep circular deps by loading context and kclManager only from React-land * Update broken import paths * Separate loaders from Router, load settings on every route * Break apart settings types, utils, and constants * Fix Jest tests by decoupling reliance on useLoaderData from SettingsAuthProvider * Fix up Router loader data with "layout routes" https://reactrouter.com/en/main/route/route#layout-routes * Move settings validation and toast to custom hook so the toast renders * fmt * Use forks for Vitest https://vitest.dev/guide/common-errors.html#failed-to-terminate-worker * $APPCONFIG !== $APPDATA only on Linux + change the identifier back since it really doesn't seem to affect app signing * Debugging on Linux * Better directory validation, fix reset settings button * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * defaultDirectory can be empty in browser * fmt * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * re-trigger CI --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-03-14 15:56:45 -04:00
import { SettingsAuthProviderJest } from './SettingsAuthProvider'
type User = Models['User_type']
describe('UserSidebarMenu tests', () => {
test("Renders user's name and email if available", async () => {
const userWellFormed: User = {
id: '8675309',
name: 'Test User',
email: 'kittycad.sidebar.test@example.com',
image: 'https://placekitten.com/200/200',
created_at: 'yesteryear',
updated_at: 'today',
company: 'Test Company',
discord: 'Test User#1234',
github: 'testuser',
phone: '555-555-5555',
first_name: 'Test',
last_name: 'User',
2024-02-29 11:57:47 +11:00
can_train_on_data: false,
is_service_account: false,
}
render(
<TestWrap>
<UserSidebarMenu user={userWellFormed} />
</TestWrap>
)
fireEvent.click(screen.getByTestId('user-sidebar-toggle'))
await waitFor(() => {
expect(screen.getByTestId('username')).toHaveTextContent(
userWellFormed.name || ''
)
})
await waitFor(() => {
expect(screen.getByTestId('email')).toHaveTextContent(
userWellFormed.email
)
})
})
test("Renders just the user's email if no name is available", async () => {
const userNoName: User = {
id: '8675309',
email: 'kittycad.sidebar.test@example.com',
image: 'https://placekitten.com/200/200',
created_at: 'yesteryear',
updated_at: 'today',
company: 'Test Company',
discord: 'Test User#1234',
github: 'testuser',
phone: '555-555-5555',
first_name: '',
last_name: '',
name: '',
2024-02-29 11:57:47 +11:00
can_train_on_data: false,
is_service_account: false,
}
render(
<TestWrap>
<UserSidebarMenu user={userNoName} />
</TestWrap>
)
fireEvent.click(screen.getByTestId('user-sidebar-toggle'))
await waitFor(() => {
expect(screen.getByTestId('username')).toHaveTextContent(userNoName.email)
})
})
test('Renders a menu button if no user avatar is available', async () => {
const userNoAvatar: User = {
id: '8675309',
name: 'Test User',
email: 'kittycad.sidebar.test@example.com',
created_at: 'yesteryear',
updated_at: 'today',
company: 'Test Company',
discord: 'Test User#1234',
github: 'testuser',
phone: '555-555-5555',
first_name: 'Test',
last_name: 'User',
image: '',
2024-02-29 11:57:47 +11:00
can_train_on_data: false,
is_service_account: false,
}
render(
<TestWrap>
<UserSidebarMenu user={userNoAvatar} />
</TestWrap>
)
await waitFor(() => {
expect(screen.getByTestId('user-sidebar-toggle')).toHaveTextContent(
'User menu'
)
})
})
})
function TestWrap({ children }: { children: React.ReactNode }) {
// wrap in router and xState context
// We have to use a memory router in the testing environment,
// and we have to use the createMemoryRouter function instead of <MemoryRouter /> as of react-router v6.4:
// https://reactrouter.com/en/6.16.0/routers/picking-a-router#using-v64-data-apis
const router = createMemoryRouter(
createRoutesFromElements(
<Route
path="/file/:id"
element={
<SettingsAuthProviderJest>{children}</SettingsAuthProviderJest>
}
/>
),
{
initialEntries: ['/file/new'],
initialIndex: 0,
}
)
return <RouterProvider router={router} />
}