Skip to content

Commit b24d8b3

Browse files
committed
test: mock connector for e2e tests
1 parent 08ff6a7 commit b24d8b3

15 files changed

+2787
-0
lines changed

CONTRIBUTING.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,56 @@ pnpm test:browser:ui # Run with Playwright UI
347347

348348
Make sure to read about [Playwright best practices](https://playwright.dev/docs/best-practices) and don't rely on classes/IDs but try to follow user-replicable behaviour (like selecting an element based on text content instead).
349349

350+
### Testing connector features
351+
352+
Features that require authentication through the local connector (org management, package collaborators, operations queue) are tested using a mock connector server. The testing infrastructure includes:
353+
354+
**For Vitest component tests** (`test/nuxt/`):
355+
356+
- Mock the `useConnector` composable with reactive state
357+
- Use `document.body` queries for components using Teleport
358+
- See `test/nuxt/components/ConnectorModal.spec.ts` for an example
359+
360+
```typescript
361+
// Create mock state
362+
const mockState = ref({ connected: false, npmUser: null, ... })
363+
364+
// Mock the composable
365+
vi.mock('~/composables/useConnector', () => ({
366+
useConnector: () => ({
367+
isConnected: computed(() => mockState.value.connected),
368+
// ... other properties
369+
}),
370+
}))
371+
```
372+
373+
**For Playwright E2E tests** (`tests/`):
374+
375+
- A mock HTTP server (`tests/helpers/mock-connector.ts`) implements the connector API
376+
- The server starts automatically via Playwright's global setup
377+
- Use the `mockConnector` fixture to set up test data and the `gotoConnected` helper to navigate with authentication
378+
379+
```typescript
380+
test('shows org members', async ({ page, gotoConnected, mockConnector }) => {
381+
// Set up test data
382+
await mockConnector.setOrgData('@testorg', {
383+
users: { testuser: 'owner', member1: 'admin' },
384+
})
385+
386+
// Navigate with connector authentication
387+
await gotoConnected('/@testorg')
388+
389+
// Test assertions
390+
await expect(page.getByRole('link', { name: '@testuser' })).toBeVisible()
391+
})
392+
```
393+
394+
The mock connector supports test endpoints for state manipulation:
395+
396+
- `/__test__/org/:org` - Set org users and teams
397+
- `/__test__/user/orgs` - Set user's organizations
398+
- `/__test__/operations` - Get/manipulate operation queue
399+
350400
## Submitting changes
351401

352402
### Before submitting

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"@voidzero-dev/vite-plus-core": "latest",
7777
"@vue/test-utils": "2.4.6",
7878
"axe-core": "^4.11.1",
79+
"h3-next": "npm:h3@2.0.1-rc.11",
7980
"happy-dom": "20.3.5",
8081
"lint-staged": "16.2.7",
8182
"marked": "17.0.1",

playwright.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export default defineConfig<ConfigOptions>({
1111
workers: process.env.CI ? 1 : undefined,
1212
reporter: 'html',
1313
timeout: 120_000,
14+
// Start/stop mock connector server before/after all tests
15+
globalSetup: fileURLToPath(new URL('./tests/global-setup.ts', import.meta.url)),
16+
globalTeardown: fileURLToPath(new URL('./tests/global-teardown.ts', import.meta.url)),
1417
use: {
1518
trace: 'on-first-retry',
1619
nuxt: {

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shared/test-utils/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Shared test utilities for mock connector testing.
3+
*
4+
* These utilities can be used by both:
5+
* - Playwright E2E tests (via HTTP server)
6+
* - Vitest browser tests (via composable mock)
7+
*/
8+
9+
// Types
10+
export * from './mock-connector-types'
11+
12+
// State management (used by both HTTP server and composable mock)
13+
export {
14+
MockConnectorStateManager,
15+
createMockConnectorState,
16+
DEFAULT_MOCK_CONFIG,
17+
} from './mock-connector-state'
18+
19+
// Composable mock (for Vitest browser tests)
20+
export {
21+
createMockConnectorComposable,
22+
type MockConnectorComposable,
23+
type MockConnectorTestControls,
24+
} from './mock-connector-composable'

0 commit comments

Comments
 (0)