-
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.
Merge branch 'main' into 13916-StudioDragAndDrop
- Loading branch information
Showing
17 changed files
with
356 additions
and
19 deletions.
There are no files selected for viewing
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
69 changes: 69 additions & 0 deletions
69
frontend/libs/studio-hooks/src/hooks/useOrgAppScopedStorage.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,69 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { type UseOrgAppScopedStorage, useOrgAppScopedStorage } from './useOrgAppScopedStorage'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
useParams: jest.fn(), | ||
})); | ||
|
||
const mockedOrg: string = 'testOrg'; | ||
const mockedApp: string = 'testApp'; | ||
const scopedStorageKey: string = 'testOrg-testApp'; | ||
const storagesToTest: Array<UseOrgAppScopedStorage['storage']> = ['localStorage', 'sessionStorage']; | ||
|
||
describe('useOrgAppScopedStorage', () => { | ||
afterEach(() => { | ||
window.localStorage.clear(); | ||
window.sessionStorage.clear(); | ||
}); | ||
|
||
it.each(storagesToTest)( | ||
'initializes ScopedStorageImpl with correct storage scope, %s', | ||
(storage) => { | ||
const { result } = renderUseOrgAppScopedStorage({ storage }); | ||
|
||
result.current.setItem('key', 'value'); | ||
|
||
expect(result.current.setItem).toBeDefined(); | ||
expect(result.current.getItem).toBeDefined(); | ||
expect(result.current.removeItem).toBeDefined(); | ||
expect(window[storage].getItem(scopedStorageKey)).toBe('{"key":"value"}'); | ||
}, | ||
); | ||
|
||
it.each(storagesToTest)('should retrieve parsed objects from %s', (storage) => { | ||
const { result } = renderUseOrgAppScopedStorage({ storage }); | ||
|
||
result.current.setItem('person', { name: 'John', age: 18 }); | ||
|
||
expect(result.current.getItem('person')).toEqual({ | ||
name: 'John', | ||
age: 18, | ||
}); | ||
}); | ||
|
||
it.each(storagesToTest)('should be possible to remove item from %s', (storage) => { | ||
const { result } = renderUseOrgAppScopedStorage({ storage }); | ||
|
||
result.current.setItem('key', 'value'); | ||
result.current.removeItem('key'); | ||
expect(result.current.getItem('key')).toBeUndefined(); | ||
}); | ||
|
||
it('should use localStorage as default storage', () => { | ||
const { result } = renderUseOrgAppScopedStorage({}); | ||
result.current.setItem('key', 'value'); | ||
|
||
expect(window.localStorage.getItem(scopedStorageKey)).toBe('{"key":"value"}'); | ||
}); | ||
}); | ||
|
||
const renderUseOrgAppScopedStorage = ({ storage }: UseOrgAppScopedStorage) => { | ||
(useParams as jest.Mock).mockReturnValue({ org: mockedOrg, app: mockedApp }); | ||
const { result } = renderHook(() => | ||
useOrgAppScopedStorage({ | ||
storage, | ||
}), | ||
); | ||
return { result }; | ||
}; |
36 changes: 36 additions & 0 deletions
36
frontend/libs/studio-hooks/src/hooks/useOrgAppScopedStorage.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,36 @@ | ||
import { useParams } from 'react-router-dom'; | ||
import { | ||
type ScopedStorage, | ||
type ScopedStorageResult, | ||
ScopedStorageImpl, | ||
} from '@studio/pure-functions'; | ||
|
||
type OrgAppParams = { | ||
org: string; | ||
app: string; | ||
}; | ||
|
||
const supportedStorageMap: Record<UseOrgAppScopedStorage['storage'], ScopedStorage> = { | ||
localStorage: window.localStorage, | ||
sessionStorage: window.sessionStorage, | ||
}; | ||
|
||
export type UseOrgAppScopedStorage = { | ||
storage?: 'localStorage' | 'sessionStorage'; | ||
}; | ||
|
||
type UseOrgAppScopedStorageResult = ScopedStorageResult; | ||
export const useOrgAppScopedStorage = ({ | ||
storage = 'localStorage', | ||
}: UseOrgAppScopedStorage): UseOrgAppScopedStorageResult => { | ||
const { org, app } = useParams<OrgAppParams>(); | ||
|
||
const storageKey: string = `${org}-${app}`; | ||
const scopedStorage = new ScopedStorageImpl(supportedStorageMap[storage], storageKey); | ||
|
||
return { | ||
setItem: scopedStorage.setItem, | ||
getItem: scopedStorage.getItem, | ||
removeItem: scopedStorage.removeItem, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export { ScopedStorageImpl, type ScopedStorage } from './ScopedStorage'; | ||
export { ScopedStorageImpl, type ScopedStorage, type ScopedStorageResult } from './ScopedStorage'; |
10 changes: 10 additions & 0 deletions
10
...etForSubform/EditLayoutSet/CreateNewSubformLayoutSet/CreateNewSubformLayoutSet.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,10 @@ | ||
.savelayoutSetButton { | ||
display: flex; | ||
align-self: flex-start; | ||
border: 2px solid var(--success-color); | ||
color: var(--success-color); | ||
} | ||
|
||
.headerIcon { | ||
font-size: large; | ||
} |
67 changes: 67 additions & 0 deletions
67
...tSetForSubform/EditLayoutSet/CreateNewSubformLayoutSet/CreateNewSubformLayoutSet.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,67 @@ | ||
import React from 'react'; | ||
import { renderWithProviders } from '../../../../../../testing/mocks'; | ||
import { CreateNewSubformLayoutSet } from './CreateNewSubformLayoutSet'; | ||
import type { ComponentType } from 'app-shared/types/ComponentType'; | ||
import { textMock } from '@studio/testing/mocks/i18nMock'; | ||
import { screen, waitFor } from '@testing-library/react'; | ||
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; | ||
import { app, org } from '@studio/testing/testids'; | ||
import { QueryKey } from 'app-shared/types/QueryKey'; | ||
import { layoutSets } from 'app-shared/mocks/mocks'; | ||
import type { LayoutSets } from 'app-shared/types/api/LayoutSetsResponse'; | ||
import userEvent from '@testing-library/user-event'; | ||
import type { FormComponent } from '../../../../../../types/FormComponent'; | ||
import { AppContext } from '../../../../../../AppContext'; | ||
import { appContextMock } from '../../../../../../testing/appContextMock'; | ||
|
||
const onSubFormCreatedMock = jest.fn(); | ||
|
||
describe('CreateNewSubformLayoutSet ', () => { | ||
afterEach(jest.clearAllMocks); | ||
|
||
it('displays the card with label and input field', () => { | ||
renderCreateNewSubformLayoutSet(); | ||
const card = screen.getByRole('textbox', { | ||
name: textMock('ux_editor.component_properties.subform.created_layout_set_name'), | ||
}); | ||
|
||
expect(card).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays the input field', () => { | ||
renderCreateNewSubformLayoutSet(); | ||
const input = screen.getByRole('textbox'); | ||
expect(input).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays the save button', () => { | ||
renderCreateNewSubformLayoutSet(); | ||
const saveButton = screen.getByRole('button', { name: textMock('general.close') }); | ||
expect(saveButton).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls onSubFormCreated when save button is clicked', async () => { | ||
const user = userEvent.setup(); | ||
renderCreateNewSubformLayoutSet(); | ||
const input = screen.getByRole('textbox'); | ||
await user.type(input, 'NewSubForm'); | ||
const saveButton = screen.getByRole('button', { name: textMock('general.close') }); | ||
await user.click(saveButton); | ||
await waitFor(() => expect(onSubFormCreatedMock).toHaveBeenCalledTimes(1)); | ||
expect(onSubFormCreatedMock).toHaveBeenCalledWith('NewSubForm'); | ||
}); | ||
}); | ||
|
||
const renderCreateNewSubformLayoutSet = ( | ||
layoutSetsMock: LayoutSets = layoutSets, | ||
componentProps: Partial<FormComponent<ComponentType.Subform>> = {}, | ||
) => { | ||
const queryClient = createQueryClientMock(); | ||
queryClient.setQueryData([QueryKey.LayoutSets, org, app], layoutSetsMock); | ||
return renderWithProviders( | ||
<AppContext.Provider value={{ ...appContextMock }}> | ||
<CreateNewSubformLayoutSet onSubFormCreated={onSubFormCreatedMock} {...componentProps} /> | ||
</AppContext.Provider>, | ||
{ queryClient }, | ||
); | ||
}; |
61 changes: 61 additions & 0 deletions
61
...LayoutSetForSubform/EditLayoutSet/CreateNewSubformLayoutSet/CreateNewSubformLayoutSet.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,61 @@ | ||
import React, { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { StudioButton, StudioCard, StudioTextfield } from '@studio/components'; | ||
import { ClipboardIcon, CheckmarkIcon } from '@studio/icons'; | ||
import { useAddLayoutSetMutation } from 'app-development/hooks/mutations/useAddLayoutSetMutation'; | ||
import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams'; | ||
import classes from './CreateNewSubformLayoutSet.module.css'; | ||
|
||
type CreateNewSubformLayoutSetProps = { | ||
onSubFormCreated: (layoutSetName: string) => void; | ||
}; | ||
|
||
export const CreateNewSubformLayoutSet = ({ | ||
onSubFormCreated, | ||
}: CreateNewSubformLayoutSetProps): React.ReactElement => { | ||
const { t } = useTranslation(); | ||
const [newSubForm, setNewSubForm] = useState(''); | ||
const { org, app } = useStudioEnvironmentParams(); | ||
const { mutate: addLayoutSet } = useAddLayoutSetMutation(org, app); | ||
|
||
const createNewSubform = () => { | ||
if (!newSubForm) return; | ||
addLayoutSet({ | ||
layoutSetIdToUpdate: newSubForm, | ||
layoutSetConfig: { | ||
id: newSubForm, | ||
type: 'subform', | ||
}, | ||
}); | ||
onSubFormCreated(newSubForm); | ||
setNewSubForm(''); | ||
}; | ||
|
||
function handleChange(e: React.ChangeEvent<HTMLInputElement>) { | ||
setNewSubForm(e.target.value); | ||
} | ||
|
||
return ( | ||
<StudioCard> | ||
<StudioCard.Content> | ||
<StudioCard.Header> | ||
<ClipboardIcon className={classes.headerIcon} /> | ||
</StudioCard.Header> | ||
<StudioTextfield | ||
label={t('ux_editor.component_properties.subform.created_layout_set_name')} | ||
value={newSubForm} | ||
size='sm' | ||
onChange={handleChange} | ||
/> | ||
<StudioButton | ||
className={classes.savelayoutSetButton} | ||
icon={<CheckmarkIcon />} | ||
onClick={createNewSubform} | ||
title={t('general.close')} | ||
variant='tertiary' | ||
color='success' | ||
/> | ||
</StudioCard.Content> | ||
</StudioCard> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
...PropertiesHeader/EditLayoutSetForSubform/EditLayoutSet/CreateNewSubformLayoutSet/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 { CreateNewSubformLayoutSet } from './CreateNewSubformLayoutSet'; |
4 changes: 4 additions & 0 deletions
4
...roperties/PropertiesHeader/EditLayoutSetForSubform/EditLayoutSet/EditLayoutSet.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,4 @@ | ||
.button { | ||
padding-left: 0; | ||
border-radius: var(--fds-sizing-1); | ||
} |
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
Oops, something went wrong.