-
Notifications
You must be signed in to change notification settings - Fork 1
Sync tenant selector dropdown after tenant save #970
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
Open
rchlfryn
wants to merge
6
commits into
main
Choose a base branch
from
fix-tenant-selector-refresh
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
340ecd2
Sync tenant selector dropdown after tenant save
rchlfryn 5eed1c0
Remove step from onboarding doc
rchlfryn e374d56
add tests for tenant selector on dashboard and check if tenant select…
rchlfryn c907be3
fix failing test
rchlfryn 8527426
Fix failing test
rchlfryn a340ec4
Fix test to make new temp tenant
rchlfryn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { expect, tenantSelectorTest as test } from '../../fixtures/tenant-selector.fixture' | ||
| import { AdminUrlUtil, CollectionSlugs } from '../../helpers' | ||
|
|
||
| /** | ||
| * Dashboard Tests | ||
| * | ||
| * Tests that the tenant selector is visible and functional on the admin dashboard. | ||
| * Previously, the selector was hidden on the dashboard because viewType was never | ||
| * set to 'dashboard'. Fixed by detecting dashboard from URL params instead. | ||
| * | ||
| * Related: https://github.com/NWACus/web/issues/691 | ||
| */ | ||
|
|
||
| test.describe.configure({ timeout: 90000 }) | ||
|
|
||
| test.describe('Tenant selector on dashboard', () => { | ||
| test('should be visible on the dashboard for super admin', async ({ | ||
| loginAs, | ||
| isTenantSelectorVisible, | ||
| getTenantOptions, | ||
| }) => { | ||
| const page = await loginAs('superAdmin') | ||
| const url = new AdminUrlUtil('http://localhost:3000', CollectionSlugs.pages) | ||
|
|
||
| await page.goto(url.dashboard) | ||
| await page.waitForLoadState('networkidle') | ||
|
|
||
| const isVisible = await isTenantSelectorVisible(page) | ||
| expect(isVisible).toBe(true) | ||
|
|
||
| const options = await getTenantOptions(page) | ||
| expect(options.length).toBeGreaterThanOrEqual(4) | ||
|
|
||
| await page.context().close() | ||
| }) | ||
|
|
||
| test('should be visible on the dashboard for multi-tenant admin', async ({ | ||
| loginAs, | ||
| isTenantSelectorVisible, | ||
| getTenantOptions, | ||
| }) => { | ||
| const page = await loginAs('multiTenantAdmin') | ||
| const url = new AdminUrlUtil('http://localhost:3000', CollectionSlugs.pages) | ||
|
|
||
| await page.goto(url.dashboard) | ||
| await page.waitForLoadState('networkidle') | ||
|
|
||
| const isVisible = await isTenantSelectorVisible(page) | ||
| expect(isVisible).toBe(true) | ||
|
|
||
| const options = await getTenantOptions(page) | ||
| expect(options.length).toBe(2) // NWAC and SNFAC | ||
|
|
||
| await page.context().close() | ||
| }) | ||
|
|
||
| test('should be hidden on dashboard for single-tenant users', async ({ | ||
| loginAs, | ||
| isTenantSelectorVisible, | ||
| }) => { | ||
| const page = await loginAs('singleTenantAdmin') | ||
| const url = new AdminUrlUtil('http://localhost:3000', CollectionSlugs.pages) | ||
|
|
||
| await page.goto(url.dashboard) | ||
| await page.waitForLoadState('networkidle') | ||
|
|
||
| // Single-tenant users have only 1 option, so selector is hidden | ||
| const isVisible = await isTenantSelectorVisible(page) | ||
| expect(isVisible).toBe(false) | ||
|
|
||
| await page.context().close() | ||
| }) | ||
| }) |
106 changes: 106 additions & 0 deletions
106
__tests__/e2e/admin/tenant-selector/sync-on-save.e2e.spec.ts
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,106 @@ | ||
| import { openNav } from '../../fixtures/nav.fixture' | ||
| import { expect, tenantSelectorTest as test } from '../../fixtures/tenant-selector.fixture' | ||
| import { AdminUrlUtil, CollectionSlugs, saveDocAndAssert, waitForFormReady } from '../../helpers' | ||
|
|
||
| /** | ||
| * Sync on Save Tests | ||
| * | ||
| * Tests that the tenant selector dropdown refreshes automatically when a | ||
| * tenant is saved (created or updated), without requiring a page reload. | ||
| * | ||
| * The SyncTenantsOnSave component watches useDocumentInfo().lastUpdateTime | ||
| * and calls syncTenants() when it changes. | ||
| */ | ||
|
|
||
| const TEMP_TENANT_SLUG_CREATE = 'e2e-sync-create' | ||
| const TEMP_TENANT_NAME_CREATE = 'E2E Sync Create Center' | ||
|
|
||
| const TEMP_TENANT_SLUG_EDIT = 'e2e-sync-edit' | ||
| const TEMP_TENANT_NAME_EDIT = 'E2E Sync Edit Center' | ||
| const UPDATED_TENANT_NAME = 'E2E Sync Edit Renamed' | ||
|
|
||
| test.describe.configure({ timeout: 90000 }) | ||
|
|
||
| test.describe('Tenant selector syncs on save', () => { | ||
| test('should show new tenant in selector after creation', async ({ | ||
| loginAs, | ||
| getTenantOptions, | ||
| }) => { | ||
| const page = await loginAs('superAdmin') | ||
| const tenantsUrl = new AdminUrlUtil('http://localhost:3000', CollectionSlugs.tenants) | ||
|
|
||
| try { | ||
| // Navigate to create a new tenant | ||
| await page.goto(tenantsUrl.create) | ||
| await page.waitForLoadState('networkidle') | ||
| await waitForFormReady(page) | ||
|
|
||
| await page.locator('#field-name').fill(TEMP_TENANT_NAME_CREATE) | ||
| await page.locator('#field-slug').fill(TEMP_TENANT_SLUG_CREATE) | ||
| await saveDocAndAssert(page) | ||
|
|
||
| // Navigate to a tenant-scoped collection via client-side nav link (NOT page.goto) | ||
| await openNav(page) | ||
| await Promise.all([ | ||
| page.waitForURL('**/admin/collections/pages'), | ||
| page.locator('nav a[href="/admin/collections/pages"]').click(), | ||
| ]) | ||
| await page.waitForLoadState('networkidle') | ||
|
|
||
| // The tenant selector should show the new tenant without a full page reload | ||
| const options = await getTenantOptions(page) | ||
| expect(options).toContain(TEMP_TENANT_NAME_CREATE) | ||
| } finally { | ||
| // Clean up: delete the temporary tenant | ||
| const response = await page.request.get( | ||
| `http://localhost:3000/api/tenants?where[slug][equals]=${TEMP_TENANT_SLUG_CREATE}&limit=1`, | ||
| ) | ||
| const data = await response.json() | ||
| if (data.docs?.[0]) { | ||
| await page.request.delete(`http://localhost:3000/api/tenants/${data.docs[0].id}`) | ||
| } | ||
| await page.context().close() | ||
| } | ||
| }) | ||
|
|
||
| test('should update tenant name in selector after editing', async ({ | ||
| loginAs, | ||
| getTenantOptions, | ||
| }) => { | ||
| const page = await loginAs('superAdmin') | ||
| const tenantsUrl = new AdminUrlUtil('http://localhost:3000', CollectionSlugs.tenants) | ||
|
|
||
| // Create a temporary tenant via API | ||
| const createResponse = await page.request.post('http://localhost:3000/api/tenants', { | ||
| data: { name: TEMP_TENANT_NAME_EDIT, slug: TEMP_TENANT_SLUG_EDIT }, | ||
| }) | ||
| const { doc: tenant } = await createResponse.json() | ||
|
|
||
| try { | ||
| // Navigate to the tenant edit page | ||
| await page.goto(tenantsUrl.edit(tenant.id)) | ||
| await page.waitForLoadState('networkidle') | ||
| await waitForFormReady(page) | ||
|
|
||
| // Rename the tenant | ||
| await page.locator('#field-name').fill(UPDATED_TENANT_NAME) | ||
| await saveDocAndAssert(page) | ||
|
|
||
| // Navigate to a tenant-scoped collection via client-side nav link (NOT page.goto) | ||
| await openNav(page) | ||
| await Promise.all([ | ||
| page.waitForURL('**/admin/collections/pages'), | ||
| page.locator('nav a[href="/admin/collections/pages"]').click(), | ||
| ]) | ||
| await page.waitForLoadState('networkidle') | ||
|
|
||
| const options = await getTenantOptions(page) | ||
| expect(options).toContain(UPDATED_TENANT_NAME) | ||
| expect(options).not.toContain(TEMP_TENANT_NAME_EDIT) | ||
| } finally { | ||
| // Clean up: delete the temporary tenant | ||
| await page.request.delete(`http://localhost:3000/api/tenants/${tenant.id}`) | ||
| await page.context().close() | ||
| } | ||
| }) | ||
| }) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,25 @@ | ||
| 'use client' | ||
|
|
||
| import { useDocumentInfo } from '@payloadcms/ui' | ||
| import { useEffect, useRef } from 'react' | ||
|
|
||
| import { useTenantSelection } from '@/providers/TenantSelectionProvider/index.client' | ||
|
|
||
| /** | ||
| * Invisible component placed on the Tenants edit view that syncs the tenant | ||
| * selector dropdown whenever a tenant document is saved (created or updated). | ||
| */ | ||
| export const SyncTenantsOnSave = () => { | ||
| const { lastUpdateTime } = useDocumentInfo() | ||
| const { syncTenants } = useTenantSelection() | ||
| const prevUpdateTime = useRef(lastUpdateTime) | ||
|
|
||
| useEffect(() => { | ||
| if (lastUpdateTime !== prevUpdateTime.current) { | ||
| prevUpdateTime.current = lastUpdateTime | ||
| syncTenants() | ||
| } | ||
| }, [lastUpdateTime, syncTenants]) | ||
|
|
||
| return null | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the renaming of dvac isn't being correctly cleaned up somehow.
I'm getting the following test failure when running e2e tests locally. This only happens when running right after a
pnpm seed. If I run this test, get the failure, then run again, it succeeds. So run a fresh seed first.pnpm seedpnpm devpnpm test:e2e __tests__/e2e/admin/tenant-selectoradmin/tenant-selector/tenant-cookie-edge-cases.e2e.spec.ts:86