-
Notifications
You must be signed in to change notification settings - Fork 86
Studio: Delete all active demo sites #131
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b8dcef1
Studio: Delete all active demo sites
kozer 8038fef
Update: Fix some linting errors
kozer 5ae3c22
Update: wording at delete all confirmation dialog
kozer ff4f14b
Update: fix linting errors
kozer 5bdd011
Update: Add a new hook to fetch snaphsots from server
kozer a2161ee
Update: Add tests
kozer a78f8d4
Fix: failing tests
kozer 7d579b1
Merge branch 'trunk' into fix/delete_all_active_demo_sites
kozer 07154b2
Update: rewording message
kozer 8751571
Update: add notification after deleting all sites
kozer 6d26231
Update: Fix lint errors
kozer 81313a0
Merge branch 'trunk' into fix/delete_all_active_demo_sites
kozer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // To run tests, execute `npm run test -- src/hooks/tests/use-fetch-snapshots.test.ts` from the root directory | ||
| import * as Sentry from '@sentry/electron/renderer'; | ||
| import { waitFor, renderHook } from '@testing-library/react'; | ||
| import { useOffline } from '../../hooks/use-offline'; | ||
| import { useAuth } from '../use-auth'; | ||
| import { useFetchSnapshots } from '../use-fetch-snaphsots'; | ||
| import { useSiteUsage } from '../use-site-usage'; | ||
|
|
||
| jest.mock( '@sentry/electron/renderer' ); | ||
| jest.mock( '../use-auth' ); | ||
| jest.mock( '../use-site-usage' ); | ||
|
|
||
| describe( 'useFetchSnapshots', () => { | ||
| // Mock data and responses | ||
| const clientReqGet = jest.fn(); | ||
|
|
||
| beforeEach( () => { | ||
| jest.resetAllMocks(); | ||
| ( useSiteUsage as jest.Mock ).mockReturnValue( { siteCount: 1 } ); | ||
| } ); | ||
| it( 'sets initial state correctly when client is not available', async () => { | ||
| ( useAuth as jest.Mock ).mockImplementation( () => ( { | ||
| client: null, | ||
| } ) ); | ||
| const { result } = renderHook( () => useFetchSnapshots() ); | ||
|
|
||
| expect( result.current.isLoading ).toBe( false ); | ||
| expect( result.current.allSnapshots ).toBe( null ); | ||
| } ); | ||
| it( 'handles fetch snapshots failure', async () => { | ||
| const error = new Error( 'Failed to fetch' ); | ||
| ( useAuth as jest.Mock ).mockReturnValue( { | ||
| client: { | ||
| req: { | ||
| get: clientReqGet.mockRejectedValue( error ), | ||
| }, | ||
| }, | ||
| } ); | ||
|
|
||
| const { result } = renderHook( () => useFetchSnapshots() ); | ||
|
|
||
| await waitFor( () => { | ||
| expect( result.current.isLoading ).toBe( false ); | ||
| expect( Sentry.captureException ).toHaveBeenCalledWith( error ); | ||
| expect( result.current.allSnapshots ).toBe( null ); | ||
| } ); | ||
| } ); | ||
| it( 'fetches snapshots on mount when client is available', async () => { | ||
| ( useAuth as jest.Mock ).mockReturnValue( { | ||
| client: { | ||
| req: { | ||
| get: clientReqGet.mockResolvedValue( { | ||
| sites: [ | ||
| { | ||
| atomic_site_id: 12345, | ||
| }, | ||
| { | ||
| atomic_site_id: 67890, | ||
| }, | ||
| { | ||
| atomic_site_id: 13579, | ||
| }, | ||
| ], | ||
| } ), | ||
| }, | ||
| }, | ||
| } ); | ||
|
|
||
| const { result } = renderHook( () => useFetchSnapshots() ); | ||
|
|
||
| await waitFor( () => { | ||
| expect( result.current.isLoading ).toBe( false ); | ||
| expect( clientReqGet ).toHaveBeenCalled(); | ||
| expect( result.current.allSnapshots ).toStrictEqual( [ | ||
| { atomicSiteId: 12345 }, | ||
| { atomicSiteId: 67890 }, | ||
| { atomicSiteId: 13579 }, | ||
| ] ); | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'should not check for snapshots when lacking an internet connection', () => { | ||
| ( useAuth as jest.Mock ).mockReturnValue( { | ||
| client: { | ||
| req: { | ||
| get: clientReqGet.mockResolvedValue( { sites: [] } ), | ||
| }, | ||
| }, | ||
| } ); | ||
| ( useOffline as jest.Mock ).mockReturnValue( true ); | ||
| renderHook( () => useSiteUsage() ); | ||
|
|
||
| expect( clientReqGet ).not.toHaveBeenCalled(); | ||
| } ); | ||
| } ); |
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,56 @@ | ||
| import * as Sentry from '@sentry/electron/renderer'; | ||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { useAuth } from './use-auth'; | ||
| import { useOffline } from './use-offline'; | ||
| import { useSiteUsage } from './use-site-usage'; | ||
|
|
||
| interface FetchSnapshotResponse { | ||
| sites: { atomic_site_id: number }[]; | ||
| } | ||
|
|
||
| export function useFetchSnapshots() { | ||
| const [ allSnapshots, setAllSnapshots ] = useState< Pick< Snapshot, 'atomicSiteId' >[] | null >( | ||
| null | ||
| ); | ||
| const { siteCount } = useSiteUsage(); | ||
| const [ isLoading, setIsLoading ] = useState( false ); | ||
| const { client } = useAuth(); | ||
| const isOffline = useOffline(); | ||
|
|
||
| const fetchAllSnapshots = useCallback( async () => { | ||
| if ( ! client?.req || isOffline ) { | ||
| return null; | ||
| } | ||
| setIsLoading( true ); | ||
| try { | ||
| const response: FetchSnapshotResponse = await client.req.get( { | ||
| path: '/jurassic-ninja/list', | ||
| apiNamespace: 'wpcom/v2', | ||
| } ); | ||
| const sites: Pick< Snapshot, 'atomicSiteId' >[] = | ||
| response.sites?.map( ( { atomic_site_id } ) => ( { atomicSiteId: atomic_site_id } ) ) ?? []; | ||
| return sites; | ||
| } catch ( error ) { | ||
| Sentry.captureException( error ); | ||
| } finally { | ||
| setIsLoading( false ); | ||
| } | ||
| }, [ client?.req, isOffline ] ); | ||
|
|
||
| useEffect( () => { | ||
| if ( ! client ) { | ||
| return; | ||
| } | ||
| const fetchSnapshots = async () => { | ||
| const sites = await fetchAllSnapshots(); | ||
| if ( ! sites ) { | ||
| return; | ||
| } | ||
| setAllSnapshots( sites ); | ||
| setIsLoading( false ); | ||
| }; | ||
| fetchSnapshots(); | ||
| }, [ client, fetchAllSnapshots, siteCount ] ); | ||
|
|
||
| return { fetchAllSnapshots, allSnapshots, isLoading }; | ||
| } |
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
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.