-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor and add tests for list admin
- Loading branch information
Showing
7 changed files
with
250 additions
and
13 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
159 changes: 159 additions & 0 deletions
159
frontend/resourceadm/components/PartyListDetails/PartyListDetail.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,159 @@ | ||
import React from 'react'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { render as rtlRender, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { textMock } from '../../../testing/mocks/i18nMock'; | ||
import { queriesMock } from 'app-shared/mocks/queriesMock'; | ||
import { PartyListDetail, PartyListDetailProps } from './PartyListDetail'; | ||
import { ServicesContextProps, ServicesContextProvider } from 'app-shared/contexts/ServicesContext'; | ||
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; | ||
|
||
const mockedNavigate = jest.fn(); | ||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useNavigate: () => mockedNavigate, | ||
})); | ||
|
||
const testOrg = 'ttd'; | ||
const testEnv = 'tt02'; | ||
const testListeIdentifier = 'listid'; | ||
|
||
const defaultProps = { | ||
org: testOrg, | ||
env: testEnv, | ||
list: { | ||
env: testEnv, | ||
identifier: testListeIdentifier, | ||
name: 'Test-liste', | ||
description: 'Dette er en beskrivelse', | ||
members: [ | ||
{ | ||
orgNr: '123456789', | ||
orgName: '', | ||
isUnderenhet: false, | ||
}, | ||
], | ||
}, | ||
backUrl: '/listadmin', | ||
}; | ||
|
||
const user = userEvent.setup(); | ||
|
||
describe('PartyListDetail', () => { | ||
it('should show special name if name is not found', () => { | ||
render(); | ||
expect(screen.getByText('<navn ikke funnet>')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should show message when list is empty', () => { | ||
render({ list: { ...defaultProps.list, members: [] } }); | ||
expect(screen.getByText('Listen inneholder ingen enheter')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call service to remove member', async () => { | ||
const removePartyListMemberMock = jest.fn(); | ||
render({}, { removePartyListMember: removePartyListMemberMock }); | ||
|
||
const removeButton = screen.getByText('Fjern fra liste'); | ||
await act(() => user.click(removeButton)); | ||
|
||
expect(removePartyListMemberMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call service to add member if member is added back', async () => { | ||
const addPartyListMemberMock = jest.fn(); | ||
render({}, { addPartyListMember: addPartyListMemberMock }); | ||
|
||
const removeButton = screen.getByText('Fjern fra liste'); | ||
await act(() => user.click(removeButton)); | ||
|
||
const reAddButton = screen.getByText('Angre fjern'); | ||
await act(() => user.click(reAddButton)); | ||
|
||
expect(addPartyListMemberMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call service to update name', async () => { | ||
const updatePartyListMock = jest.fn(); | ||
render({}, { updatePartyList: updatePartyListMock }); | ||
|
||
const nameField = screen.getByLabelText('Listenavn'); | ||
await act(() => user.type(nameField, ' endret')); | ||
await act(() => nameField.blur()); | ||
|
||
expect(updatePartyListMock).toHaveBeenCalledWith(testOrg, testListeIdentifier, testEnv, [ | ||
{ op: 'replace', path: '/name', value: 'Test-liste endret' }, | ||
]); | ||
}); | ||
|
||
it('should call service to update description', async () => { | ||
const updatePartyListMock = jest.fn(); | ||
render({}, { updatePartyList: updatePartyListMock }); | ||
|
||
const descriptionField = screen.getByLabelText('Beskrivelse'); | ||
await act(() => user.type(descriptionField, ' endret')); | ||
await act(() => descriptionField.blur()); | ||
|
||
expect(updatePartyListMock).toHaveBeenCalledWith(testOrg, testListeIdentifier, testEnv, [ | ||
{ op: 'replace', path: '/description', value: 'Dette er en beskrivelse endret' }, | ||
]); | ||
}); | ||
|
||
it('should call service to remove description', async () => { | ||
const updatePartyListMock = jest.fn(); | ||
render({}, { updatePartyList: updatePartyListMock }); | ||
|
||
const descriptionField = screen.getByLabelText('Beskrivelse'); | ||
await act(() => user.clear(descriptionField)); | ||
await act(() => descriptionField.blur()); | ||
|
||
expect(updatePartyListMock).toHaveBeenCalledWith(testOrg, testListeIdentifier, testEnv, [ | ||
{ op: 'remove', path: '/description' }, | ||
]); | ||
}); | ||
|
||
it('should navigate back after list is deleted', async () => { | ||
const addPartyListMemberMock = jest.fn(); | ||
render({}, { addPartyListMember: addPartyListMemberMock }); | ||
|
||
const deleteListButton = screen.getByText('Slett liste'); | ||
await act(() => user.click(deleteListButton)); | ||
|
||
const confirmDeleteButton = screen.getAllByText('Slett liste'); | ||
await act(() => user.click(confirmDeleteButton[0])); | ||
|
||
expect(mockedNavigate).toHaveBeenCalledWith('/listadmin'); | ||
}); | ||
|
||
it('should close modal on cancel delete', async () => { | ||
const addPartyListMemberMock = jest.fn(); | ||
render({}, { addPartyListMember: addPartyListMemberMock }); | ||
|
||
const deleteListButton = screen.getByText('Slett liste'); | ||
await act(() => user.click(deleteListButton)); | ||
|
||
const cancelDeleteButton = screen.getByText('Avbryt'); | ||
await act(() => user.click(cancelDeleteButton)); | ||
|
||
expect(screen.queryByText('Bekreft sletting av liste')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
const render = ( | ||
props: Partial<PartyListDetailProps> = {}, | ||
queries: Partial<ServicesContextProps> = {}, | ||
) => { | ||
const allQueries: ServicesContextProps = { | ||
...queriesMock, | ||
...queries, | ||
}; | ||
|
||
return rtlRender( | ||
<MemoryRouter> | ||
<ServicesContextProvider {...allQueries} client={createQueryClientMock()}> | ||
<PartyListDetail {...defaultProps} {...props} /> | ||
</ServicesContextProvider> | ||
</MemoryRouter>, | ||
); | ||
}; |
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
68 changes: 68 additions & 0 deletions
68
frontend/resourceadm/components/PartyListDetails/PartyListSearch.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,68 @@ | ||
import React from 'react'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { render as rtlRender, screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { textMock } from '../../../testing/mocks/i18nMock'; | ||
import { queriesMock } from 'app-shared/mocks/queriesMock'; | ||
import { PartyListSearch } from './PartyListSearch'; | ||
import { ServicesContextProps, ServicesContextProvider } from 'app-shared/contexts/ServicesContext'; | ||
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; | ||
|
||
const enhetSearchResult = { | ||
_embedded: { | ||
enheter: [{ organisasjonsnummer: '123456789', navn: 'Digdir' }], | ||
}, | ||
}; | ||
|
||
const underenhetSearchResult = { | ||
_embedded: { | ||
underenheter: [{ organisasjonsnummer: '987654321', navn: 'Under Digdir' }], | ||
}, | ||
}; | ||
|
||
const handleAddMemberMock = jest.fn(); | ||
|
||
const defaultProps = { | ||
existingMembers: [ | ||
{ | ||
orgNr: '123456789', | ||
orgName: '', | ||
isUnderenhet: false, | ||
}, | ||
], | ||
handleAddMember: handleAddMemberMock, | ||
}; | ||
|
||
const user = userEvent.setup(); | ||
|
||
describe('PartyListSearch', () => { | ||
it('should call handleAddMember when enhet is selected', async () => { | ||
render(); | ||
|
||
const searchField = screen.getByTestId('enhet-search'); | ||
await act(() => user.type(searchField, 'Digdir')); | ||
|
||
await waitFor(() => screen.findByText('987654321 - Under Digdir')); | ||
const searchResultsButton = screen.getByText('987654321 - Under Digdir'); | ||
await act(() => user.click(searchResultsButton)); | ||
|
||
expect(handleAddMemberMock).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
const render = () => { | ||
const allQueries: ServicesContextProps = { | ||
...queriesMock, | ||
getEnheter: jest.fn().mockImplementation(() => Promise.resolve(enhetSearchResult)), | ||
getUnderenheter: jest.fn().mockImplementation(() => Promise.resolve(underenhetSearchResult)), | ||
}; | ||
|
||
return rtlRender( | ||
<MemoryRouter> | ||
<ServicesContextProvider {...allQueries} client={createQueryClientMock()}> | ||
<PartyListSearch {...defaultProps} /> | ||
</ServicesContextProvider> | ||
</MemoryRouter>, | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
export interface JsonPatch { | ||
op: 'replace' | 'add' | 'remove'; | ||
path: string; | ||
value: string | number; | ||
value?: string | number; | ||
} | ||
|
||
export const createReplacePatch = <T>(diff: T): JsonPatch[] => { | ||
return Object.keys(diff).map((key) => { | ||
const isRemove = !diff[key]; | ||
return { | ||
op: 'replace', | ||
op: isRemove ? 'remove' : 'replace', | ||
path: `/${key}`, | ||
value: diff[key], | ||
...(!isRemove && { value: diff[key] }), | ||
}; | ||
}); | ||
}; |