/
githubmirror
/
wp-calypso
Обзор
Документация
Войти
/
githubmirror
/
wp-calypso
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
client/test/root-section.ts
270 строк
7 KB
Dave Martin
Revert #95140 (#112976)
27 июл 2026, 05:15
Не верифицирован
27 июл 2026, 05:15
8de5e2b
Код
Авторство
О чём код?
/** * @jest-environment jsdom */ import { isEnabled } from '@automattic/calypso-config'; import pageLibrary from '@automattic/calypso-router'; import { waitFor } from '@testing-library/dom'; import { dashboardLink } from 'calypso/dashboard/utils/link'; import initRootSection from '../root'; const originalLocation = window.location; // Replaces `window.location` so we can assert on `assign()` for redirects to // the hosting dashboard (an absolute URL, so `root` uses `location.assign` // rather than the in-app router). function mockLocationAssign() { const assign = jest.fn(); Object.defineProperty( window, 'location', { value: { assign }, configurable: true } ); return assign; } afterEach( () => { Object.defineProperty( window, 'location', { value: originalLocation, configurable: true } ); } ); function initRouter( { state }: { state: any } ) { const dispatch = jest.fn(); const getState = jest.fn( () => state ); const page = pageLibrary.create(); page( '*', ( context, next ) => { context.store = { dispatch, getState }; next(); } ); initRootSection( null, page ); page.start(); return { dispatch, getState, page }; } describe( 'Logged In Landing Page', () => { test( 'user with no sites goes to Sites Dashboard', async () => { const state = { currentUser: { id: 55 }, sites: { items: {} }, ui: {} }; const { page } = initRouter( { state } ); page( '/' ); await waitFor( () => expect( page.current ).toBe( '/sites' ) ); } ); if ( isEnabled( 'dashboard/enable-percentage-rollout' ) ) { test( 'rollout cohort user with no sites goes to Sites Dashboard', async () => { // User ID 1 will be allocated to the new dashboard rollout const state = { currentUser: { id: 1 }, sites: { items: {} }, ui: {} }; const { page } = initRouter( { state } ); const assign = mockLocationAssign(); page( '/' ); await waitFor( () => expect( assign ).toHaveBeenCalledWith( dashboardLink( '/sites' ) ) ); } ); } test( 'user with a primary site but no permissions goes to day stats', async () => { const state = { currentUser: { id: 1, capabilities: { 1: {} }, user: { primary_blog: 1 } }, ui: {}, sites: { items: { 1: { ID: 1, URL: 'https://test.wordpress.com', }, }, }, }; const { page } = initRouter( { state } ); page( '/' ); await waitFor( () => expect( page.current ).toBe( '/stats/day/test.wordpress.com' ) ); } ); test( 'user with a primary site and edit permissions goes to My Home', async () => { const state = { currentUser: { id: 1, capabilities: { 1: { edit_posts: true } }, user: { primary_blog: 1 } }, ui: {}, sites: { items: { 1: { ID: 1, URL: 'https://test.wordpress.com', }, }, }, }; const { page } = initRouter( { state } ); page( '/' ); await waitFor( () => expect( page.current ).toBe( '/home/test.wordpress.com' ) ); } ); test( 'user with a Jetpack site set as their primary site goes to day stats', async () => { const state = { currentUser: { id: 1, capabilities: { 1: { edit_posts: true } }, user: { primary_blog: 1 } }, ui: {}, sites: { items: { 1: { ID: 1, URL: 'https://test.jurassic.ninja', jetpack: true, }, }, }, }; const { page } = initRouter( { state } ); page( '/' ); await waitFor( () => expect( page.current ).toBe( '/stats/day/test.jurassic.ninja' ) ); } ); test( 'user who opts in goes to sites page', async () => { const state = { currentUser: { id: 55, capabilities: { 1: { edit_posts: true } }, user: { primary_blog: 1, site_count: 2 }, }, preferences: { localValues: { 'sites-landing-page': { useSitesAsLandingPage: true, updatedAt: 1111 }, 'reader-landing-page': { useReaderAsLandingPage: false, updatedAt: 1111 }, }, }, ui: {}, sites: { items: { 1: { ID: 1, URL: 'https://test.wordpress.com', }, 2: { ID: 2, URL: 'https://test.jurassic.ninja', jetpack: true, }, }, }, }; const { page } = initRouter( { state } ); page( '/' ); await waitFor( () => expect( page.current ).toBe( '/sites' ) ); } ); if ( isEnabled( 'add/dashboard-enable-percentage-rollout' ) ) { test( 'rollout cohort user who opts in goes to sites page', async () => { const state = { currentUser: { id: 1, capabilities: { 1: { edit_posts: true } }, user: { primary_blog: 1, site_count: 2 }, }, preferences: { localValues: { 'sites-landing-page': { useSitesAsLandingPage: true, updatedAt: 1111 }, 'reader-landing-page': { useReaderAsLandingPage: false, updatedAt: 1111 }, }, }, ui: {}, sites: { items: { 1: { ID: 1, URL: 'https://test.wordpress.com', }, 2: { ID: 2, URL: 'https://test.jurassic.ninja', jetpack: true, }, }, }, }; const assign = mockLocationAssign(); const { page } = initRouter( { state } ); page( '/' ); await waitFor( () => expect( assign ).toHaveBeenCalledWith( dashboardLink( '/sites' ) ) ); } ); } test( 'user who opts in goes to reader page', async () => { const state = { currentUser: { id: 1, capabilities: { 1: { edit_posts: true } }, user: { primary_blog: 1, site_count: 2 }, }, preferences: { localValues: { 'sites-landing-page': { useSitesAsLandingPage: false, updatedAt: 1111 }, 'reader-landing-page': { useReaderAsLandingPage: true, updatedAt: 1111 }, }, }, ui: {}, sites: { items: { 1: { ID: 1, URL: 'https://test.wordpress.com', }, 2: { ID: 2, URL: 'https://test.jurassic.ninja', jetpack: true, }, }, }, }; const { page } = initRouter( { state } ); page( '/' ); await waitFor( () => expect( page.current ).toBe( '/reader' ) ); } ); test( 'user with a primary site using the Classic interface goes to WP Admin Dashboard', async () => { const state = { currentUser: { id: 1, capabilities: { 1: { edit_posts: true } }, user: { primary_blog: 1 }, }, ui: {}, sites: { items: { 1: { ID: 1, URL: 'https://test.wordpress.com', options: { wpcom_admin_interface: 'wp-admin', admin_url: 'https://test.wordpress.com/wp-admin/', }, }, }, }, }; const { page } = initRouter( { state } ); Object.defineProperty( window, 'location', { value: { assign: jest.fn(), }, } ); page( '/' ); await waitFor( () => { expect( window.location.assign ).toHaveBeenCalledWith( 'https://test.wordpress.com/wp-admin/' ); } ); } ); } );