-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show alert when no scope is available (#14059)
Co-authored-by: davidovrelid.com <[email protected]> Co-authored-by: David Ovrelid <[email protected]> Co-authored-by: Mirko Sekulic <[email protected]>
- Loading branch information
1 parent
2664fed
commit 6a1351e
Showing
12 changed files
with
176 additions
and
9 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
frontend/app-development/hooks/queries/useGetScopesQuery.ts
This file contains 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,12 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { QueryKey } from 'app-shared/types/QueryKey'; | ||
import { useServicesContext } from 'app-shared/contexts/ServicesContext'; | ||
import { type MaskinportenScope } from 'app-shared/types/MaskinportenScope'; | ||
|
||
export const useGetScopesQuery = () => { | ||
const { getMaskinportenScopes } = useServicesContext(); | ||
return useQuery<MaskinportenScope[]>({ | ||
queryKey: [QueryKey.AppScopes], | ||
queryFn: () => getMaskinportenScopes(), | ||
}); | ||
}; |
This file contains 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 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
3 changes: 3 additions & 0 deletions
3
...ingsModalButton/SettingsModal/components/Tabs/Maskinporten/ScopeList/ScopeList.module.css
This file contains 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,3 @@ | ||
.noScopeAlert { | ||
margin-top: var(--fds-spacing-8); | ||
} |
66 changes: 66 additions & 0 deletions
66
...ttingsModalButton/SettingsModal/components/Tabs/Maskinporten/ScopeList/ScopeList.test.tsx
This file contains 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,66 @@ | ||
import React from 'react'; | ||
import { screen, waitForElementToBeRemoved } from '@testing-library/react'; | ||
import { ScopeList } from './ScopeList'; | ||
import { textMock } from '@studio/testing/mocks/i18nMock'; | ||
import { queriesMock } from 'app-shared/mocks/queriesMock'; | ||
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; | ||
import { renderWithProviders } from 'app-development/test/mocks'; | ||
import { type MaskinportenScope } from 'app-shared/types/MaskinportenScope'; | ||
|
||
const scopesMock: MaskinportenScope = { | ||
scope: 'label', | ||
description: 'description', | ||
}; | ||
|
||
describe('ScopeList', () => { | ||
it('should display a spinner while loading', () => { | ||
renderScopeList(); | ||
expect(screen.getByTitle(textMock('general.loading'))).toBeInTheDocument(); | ||
}); | ||
|
||
it('should display a list of scopes if scopes are available', async () => { | ||
const mockGetMaskinportenScopes = jest | ||
.fn() | ||
.mockImplementation(() => Promise.resolve([scopesMock])); | ||
|
||
renderScopeList({ | ||
queries: { | ||
getMaskinportenScopes: mockGetMaskinportenScopes, | ||
}, | ||
}); | ||
|
||
await waitForGetScopesCheckIsDone(); | ||
|
||
expect( | ||
screen.getByText('List of scopes and possibility to select scope comes here'), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('should display an alert if no scopes are available', async () => { | ||
const mockGetMaskinportenScopes = jest.fn().mockImplementation(() => Promise.resolve([])); | ||
|
||
renderScopeList({ | ||
queries: { | ||
getMaskinportenScopes: mockGetMaskinportenScopes, | ||
}, | ||
}); | ||
await waitForGetScopesCheckIsDone(); | ||
|
||
expect( | ||
screen.getByText(textMock('settings_modal.maskinporten_no_scopes_available')), | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
type RenderScopeListProps = { | ||
queries?: Partial<typeof queriesMock>; | ||
}; | ||
const renderScopeList = ({ queries = queriesMock }: Partial<RenderScopeListProps> = {}) => { | ||
const queryClient = createQueryClientMock(); | ||
|
||
renderWithProviders({ ...queriesMock, ...queries }, queryClient)(<ScopeList />); | ||
}; | ||
|
||
async function waitForGetScopesCheckIsDone() { | ||
await waitForElementToBeRemoved(() => screen.queryByTitle(textMock('general.loading'))); | ||
} |
25 changes: 25 additions & 0 deletions
25
...er/SettingsModalButton/SettingsModal/components/Tabs/Maskinporten/ScopeList/ScopeList.tsx
This file contains 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 @@ | ||
import React, { type ReactElement } from 'react'; | ||
import classes from './ScopeList.module.css'; | ||
import { StudioAlert, StudioSpinner } from '@studio/components'; | ||
import { useGetScopesQuery } from 'app-development/hooks/queries/useGetScopesQuery'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
export const ScopeList = (): ReactElement => { | ||
const { t } = useTranslation(); | ||
const { data: scopes, isPending: isPendingScopes } = useGetScopesQuery(); | ||
const hasScopes: boolean = scopes?.length > 0; | ||
|
||
if (isPendingScopes) { | ||
return <StudioSpinner spinnerTitle={t('general.loading')} />; | ||
} | ||
|
||
if (hasScopes) { | ||
return <div>List of scopes and possibility to select scope comes here</div>; | ||
} | ||
|
||
return ( | ||
<StudioAlert severity='info' className={classes.noScopeAlert}> | ||
{t('settings_modal.maskinporten_no_scopes_available')} | ||
</StudioAlert> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
...bHeader/SettingsModalButton/SettingsModal/components/Tabs/Maskinporten/ScopeList/index.ts
This file contains 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 @@ | ||
export { ScopeList } from './ScopeList'; |
This file contains 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 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 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 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,4 @@ | ||
export type MaskinportenScope = { | ||
scope: string; | ||
description: string; | ||
}; |
This file contains 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