-
Notifications
You must be signed in to change notification settings - Fork 181
test: add mobile sdk demo vitest tests #1203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
transphorm
merged 7 commits into
dev
from
codex/reapply-vitest/jsdom-setup-for-mobile-sdk-demo
Oct 4, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6b755f2
test: restore mobile sdk demo vitest suites
transphorm 6534153
save fixes
transphorm c5e603b
test: expand mobile sdk demo screen coverage
transphorm 9d55097
Merge branch 'codex/reapply-vitest/jsdom-setup-for-mobile-sdk-demo' o…
transphorm 69bbf32
fix lint and types
transphorm 7201771
update packages
transphorm c0c44cd
reset test
transphorm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,198 @@ | ||
| import React from 'react'; | ||
| import { render, screen, waitFor, act } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { sdkMocks } from './mocks/sdk'; | ||
|
|
||
| vi.mock('../src/providers/SelfClientProvider', () => ({ | ||
| __esModule: true, | ||
| default: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| })); | ||
|
|
||
| type ScreenContext = import('../src/screens').ScreenContext; | ||
|
|
||
| let latestContext: ScreenContext | null = null; | ||
| function createScreensModule() { | ||
| const descriptors = [ | ||
| { | ||
| id: 'generate', | ||
| title: 'Generate Mock Document', | ||
| subtitle: 'Create sample passport data for testing', | ||
| sectionTitle: 'Main', | ||
| status: 'working' as const, | ||
| load: () => () => <div data-testid="screen-Generate Screen">Generate Screen</div>, | ||
| getProps: ({ navigate, refreshDocuments }: ScreenContext) => ({ | ||
| onBack: () => navigate('home'), | ||
| onNavigate: navigate, | ||
| onDocumentStored: refreshDocuments, | ||
| }), | ||
| }, | ||
| { | ||
| id: 'documents', | ||
| title: 'Document List', | ||
| subtitle: 'View and manage stored documents', | ||
| sectionTitle: 'Main', | ||
| status: 'working' as const, | ||
| load: () => () => <div data-testid="screen-Documents Screen">Documents Screen</div>, | ||
| getProps: ({ navigate, documentCatalog }: ScreenContext) => ({ | ||
| onBack: () => navigate('home'), | ||
| catalog: documentCatalog, | ||
| }), | ||
| }, | ||
| ]; | ||
|
|
||
| const screenMap = descriptors.reduce<Record<string, any>>((acc, descriptor) => { | ||
| acc[descriptor.id] = descriptor; | ||
| return acc; | ||
| }, {}); | ||
|
|
||
| const orderedSectionEntries = [{ title: 'Main', items: descriptors }]; | ||
|
|
||
| const HomeScreen = ({ screenContext }: { screenContext: ScreenContext }) => { | ||
| latestContext = screenContext; | ||
| return ( | ||
| <div> | ||
| <h1>Self Demo App</h1> | ||
| {orderedSectionEntries.map(section => ( | ||
| <section key={section.title}> | ||
| <h2>{section.title}</h2> | ||
| {section.items.map(descriptor => ( | ||
| <button | ||
| key={descriptor.id} | ||
| type="button" | ||
| onClick={() => screenContext.navigate(descriptor.id as import('../src/screens').ScreenRoute)} | ||
| > | ||
| {descriptor.title} | ||
| </button> | ||
| ))} | ||
| </section> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| return { | ||
| __esModule: true as const, | ||
| orderedSectionEntries, | ||
| screenDescriptors: descriptors, | ||
| screenMap, | ||
| HomeScreen, | ||
| }; | ||
| } | ||
|
|
||
| vi.mock('../src/screens', () => { | ||
| const moduleExports = createScreensModule(); | ||
| return moduleExports; | ||
| }); | ||
|
|
||
| vi.mock('../src/screens/HomeScreen', () => ({ | ||
| __esModule: true, | ||
| default: (props: { screenContext: ScreenContext }) => createScreensModule().HomeScreen(props), | ||
| })); | ||
|
|
||
| import App from '../App'; | ||
|
|
||
| describe('App integration', () => { | ||
| beforeEach(() => { | ||
| latestContext = null; | ||
| }); | ||
|
|
||
| it('fetches catalog and selected document on mount', async () => { | ||
| const catalog = { | ||
| documents: [{ id: 'doc-1', documentType: 'mock_passport' }], | ||
| selectedDocumentId: 'doc-1', | ||
| } as any; | ||
| const selected = { | ||
| metadata: { id: 'doc-1', documentType: 'mock_passport' }, | ||
| data: { id: 'doc-1' }, | ||
| } as any; | ||
|
|
||
| sdkMocks.selfClient.loadDocumentCatalog.mockResolvedValueOnce(catalog); | ||
| sdkMocks.loadSelectedDocumentMock.mockResolvedValueOnce(selected); | ||
|
|
||
| render(<App />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(sdkMocks.selfClient.loadDocumentCatalog).toHaveBeenCalled(); | ||
| expect(sdkMocks.loadSelectedDocumentMock).toHaveBeenCalled(); | ||
| expect(latestContext).not.toBeNull(); | ||
| }); | ||
|
|
||
| expect(screen.getByText(/Self Demo App/i)).toBeInTheDocument(); | ||
| expect(latestContext?.documentCatalog).toEqual(catalog); | ||
| expect(latestContext?.selectedDocument).toEqual(selected); | ||
| }); | ||
|
|
||
| it('navigates into descriptor screens from the home menu', async () => { | ||
| sdkMocks.selfClient.loadDocumentCatalog.mockResolvedValueOnce({ documents: [] }); | ||
| sdkMocks.loadSelectedDocumentMock.mockResolvedValueOnce(null); | ||
|
|
||
| render(<App />); | ||
|
|
||
| await screen.findByText(/Self Demo App/i); | ||
|
|
||
| await userEvent.click(screen.getByRole('button', { name: /document list/i })); | ||
|
|
||
| expect(await screen.findByTestId('screen-Documents Screen')).toHaveTextContent('Documents Screen'); | ||
|
|
||
| await act(async () => { | ||
| latestContext?.navigate('home'); | ||
| }); | ||
|
|
||
| await screen.findByText(/Self Demo App/i); | ||
| await userEvent.click(screen.getByRole('button', { name: /generate mock document/i })); | ||
|
|
||
| expect(await screen.findByTestId('screen-Generate Screen')).toHaveTextContent('Generate Screen'); | ||
| }); | ||
|
|
||
| it('falls back to the home screen when navigating to an unknown route', async () => { | ||
| sdkMocks.selfClient.loadDocumentCatalog.mockResolvedValueOnce({ documents: [] }); | ||
| sdkMocks.loadSelectedDocumentMock.mockResolvedValueOnce(null); | ||
|
|
||
| render(<App />); | ||
|
|
||
| await screen.findByText(/Self Demo App/i); | ||
|
|
||
| await act(async () => { | ||
| latestContext?.navigate('documents'); | ||
| }); | ||
|
|
||
| expect(await screen.findByTestId('screen-Documents Screen')).toBeInTheDocument(); | ||
|
|
||
| await act(async () => { | ||
| latestContext?.navigate('unknown' as any); | ||
| }); | ||
|
|
||
| await waitFor(() => expect(screen.getByText(/Self Demo App/i)).toBeInTheDocument()); | ||
| }); | ||
|
|
||
| it('resets catalog and selection when refresh fails', async () => { | ||
| const catalog = { | ||
| documents: [{ id: 'doc-2', documentType: 'mock_passport' }], | ||
| selectedDocumentId: 'doc-2', | ||
| } as any; | ||
| const selected = { | ||
| metadata: { id: 'doc-2', documentType: 'mock_passport' }, | ||
| data: { id: 'doc-2' }, | ||
| } as any; | ||
|
|
||
| sdkMocks.selfClient.loadDocumentCatalog | ||
| .mockResolvedValueOnce(catalog) | ||
| .mockRejectedValueOnce(new Error('no catalog')); | ||
| sdkMocks.loadSelectedDocumentMock.mockResolvedValueOnce(selected).mockRejectedValueOnce(new Error('no selection')); | ||
|
|
||
| render(<App />); | ||
|
|
||
| await waitFor(() => expect(latestContext?.selectedDocument).toEqual(selected)); | ||
|
|
||
| await act(async () => { | ||
| await latestContext?.refreshDocuments(); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(latestContext?.documentCatalog.documents).toEqual([]); | ||
| expect(latestContext?.selectedDocument).toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
35 changes: 35 additions & 0 deletions
35
packages/mobile-sdk-demo/tests/components/MenuButton.test.tsx
This file contains hidden or 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,35 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import MenuButton from '../../src/components/MenuButton'; | ||
|
|
||
| describe('MenuButton', () => { | ||
| it('renders the provided title and subtitle', () => { | ||
| render(<MenuButton title="Documents" subtitle="Manage your IDs" onPress={() => {}} />); | ||
|
|
||
| expect(screen.getByRole('button', { name: /documents/i })).toBeInTheDocument(); | ||
| expect(screen.getByText(/manage your ids/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('invokes onPress when pressed if enabled', async () => { | ||
| const onPress = vi.fn(); | ||
| render(<MenuButton title="Open" onPress={onPress} isWorking />); | ||
|
|
||
| await userEvent.click(screen.getByRole('button', { name: /open/i })); | ||
|
|
||
| expect(onPress).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('prevents presses and shows disabled styles when disabled', async () => { | ||
| const onPress = vi.fn(); | ||
| render(<MenuButton title="Disabled" subtitle="Unavailable" onPress={onPress} disabled />); | ||
|
|
||
| const button = screen.getByRole('button', { name: /disabled/i }); | ||
| expect(button).toBeDisabled(); | ||
|
|
||
| await userEvent.click(button); | ||
| expect(onPress).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
packages/mobile-sdk-demo/tests/components/ScreenLayout.test.tsx
This file contains hidden or 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,33 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import ScreenLayout from '../../src/components/ScreenLayout'; | ||
|
|
||
| describe('ScreenLayout', () => { | ||
| it('renders title, children, and right action content', () => { | ||
| render( | ||
| <ScreenLayout title="My Title" onBack={() => {}} rightAction={<span>Clear</span>}> | ||
| <span>Body content</span> | ||
| </ScreenLayout>, | ||
| ); | ||
|
|
||
| expect(screen.getByText(/my title/i)).toBeInTheDocument(); | ||
| expect(screen.getByText(/body content/i)).toBeInTheDocument(); | ||
| expect(screen.getByText(/clear/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('invokes onBack when the header back button is pressed', async () => { | ||
| const onBack = vi.fn(); | ||
| render( | ||
| <ScreenLayout title="Back Test" onBack={onBack}> | ||
| <span>Content</span> | ||
| </ScreenLayout>, | ||
| ); | ||
|
|
||
| await userEvent.click(screen.getByRole('button', { name: /back/i })); | ||
|
|
||
| expect(onBack).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.