Skip to content

Commit

Permalink
tests: additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmijatovic authored and cmeessen committed Aug 28, 2023
1 parent cb29903 commit 6f49303
Show file tree
Hide file tree
Showing 111 changed files with 3,269 additions and 778 deletions.
6 changes: 5 additions & 1 deletion frontend/__tests__/CookiesPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-FileCopyrightText: 2022 - 2023 Dusan Mijatovic (dv4all)
// SPDX-FileCopyrightText: 2022 - 2023 dv4all
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -8,9 +10,11 @@ import {WrappedComponentWithProps} from '../utils/jest/WrappedComponents'

import CookiesPage from '../pages/cookies'

// we mock default providers too
// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')


it('renders cookies page with title Cookies', async() => {
const props = {
matomoId: null,
Expand Down
6 changes: 6 additions & 0 deletions frontend/__tests__/Home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import Home from '../pages/index'
import {defaultRsdSettings} from '~/config/rsdSettingsReducer'
import {WrappedComponentWithProps} from '~/utils/jest/WrappedComponents'

// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')
// use DEFAULT MOCK for organisation list used by Helmholtz homepage
jest.mock('~/components/home/helmholtz/useOrganisations')

const props = {
host: {
name: 'rsd'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {render, screen, waitForElementToBeRemoved} from '@testing-library/react'
import {WithAppContext, mockSession} from '~/utils/jest/WithAppContext'

import OrganisationPage from '../pages/organisations/[...slug]'
import OrganisationPage, {OrganisationPageProps} from '../pages/organisations/[...slug]'

// MOCKS
import mockRORIinfo from './__mocks__/apiRORInfo.json'
Expand All @@ -22,13 +22,20 @@ import {TabKey} from '~/components/organisation/tabs/OrganisationTabItems'
// MOCK user agreement call
jest.mock('~/components/user/settings/fetchAgreementStatus')

// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')

const mockProps = {
organisation: mockOrganisation,
slug:['dutch-research-council'],
tab: 'software' as TabKey,
ror: mockRORIinfo as any,
isMaintainer: false
}
isMaintainer: false,
rsd_page_rows: 12,
rsd_page_layout: 'grid'
} as OrganisationPageProps

// MOCK isMaintainerOfOrganisation
const mockIsMaintainerOfOrganisation = jest.fn((props) => {
// console.log('mockIsMaintainerOfOrganisation...', props)
Expand All @@ -41,13 +48,17 @@ jest.mock('~/auth/permissions/isMaintainerOfOrganisation', () => {
default: jest.fn(props=>mockIsMaintainerOfOrganisation(props))
}
})

// MOCK getSoftwareForOrganisation
const mockSoftwareForOrganisation = jest.fn((props) => Promise.resolve({
status: 206,
count: 0,
data: []
}))
// MOCK software filters - use default mocks
jest.mock('~/components/organisation/software/filters/useOrgSoftwareKeywordsList')
jest.mock('~/components/organisation/software/filters/useOrgSoftwareLanguagesList')
jest.mock('~/components/organisation/software/filters/useOrgSoftwareLicensesList')

// MOCK getProjectsForOrganisation
const mockProjectsForOrganisation = jest.fn((props) => Promise.resolve({
status: 206,
Expand All @@ -62,9 +73,12 @@ jest.mock('~/components/organisation/apiOrganisations', () => ({
getOrganisationChildren: jest.fn((props)=>mockGetOrganisationChildren(props))
}))

// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')
// MOCK project filters - use default mocks
jest.mock('~/components/organisation/projects/filters/useOrgProjectDomainsList')
jest.mock('~/components/organisation/projects/filters/useOrgProjectKeywordsList')
jest.mock('~/components/organisation/projects/filters/useOrgProjectOrganisationsList')
jest.mock('~/components/organisation/projects/filters/useOrgProjectStatusList')


describe('pages/organisations/[...slug].tsx', () => {
beforeEach(() => {
Expand Down
2 changes: 2 additions & 0 deletions frontend/__tests__/ProjectEditPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jest.mock('~/auth/permissions/isMaintainerOf', () => ({

// MOCK useProjectToEdit (default mock)
jest.mock('~/components/projects/edit/information/useProjectToEdit')
// mock default keywords list (autocomplete)
jest.mock('~/components/projects/edit/information/searchForKeyword')

// MOCK IntersectionObserver
const mockObserve = jest.fn()
Expand Down
4 changes: 4 additions & 0 deletions frontend/__tests__/ProjectItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import {RelatedProject} from '../types/Project'
import {MentionItemProps} from '../types/Mention'
import {ProjectOrganisationProps} from '~/types/Organisation'

// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')

const mockedProps: ProjectPageProps = {
slug: 'test-slug',
project: projectItem,
Expand Down
4 changes: 4 additions & 0 deletions frontend/__tests__/ProjectsOverviewPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {LayoutType} from '~/components/software/overview/search/ViewToggleGroup'

import mockData from './__mocks__/projectsOverview.json'

// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')

const mockProps = {
search: null,
order: null,
Expand Down
6 changes: 6 additions & 0 deletions frontend/__tests__/SoftwareItem.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-FileCopyrightText: 2022 Dusan Mijatovic (dv4all)
// SPDX-FileCopyrightText: 2022 dv4all
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -10,6 +12,10 @@ import {WrappedComponentWithProps} from '../utils/jest/WrappedComponents'
// mock fetch response
import softwareIndexData from './__mocks__/softwareIndexData'

// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')

jest.mock('../utils/getSoftware')

// mock next router
Expand Down
4 changes: 4 additions & 0 deletions frontend/__tests__/SoftwareOverview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {WithAppContext} from '~/utils/jest/WithAppContext'
import SoftwareOverviewPage from '../pages/software/index'
import {LayoutType} from '~/components/software/overview/search/ViewToggleGroup'

// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')

// mocked data & props
import mockData from './__mocks__/softwareOverviewData.json'

Expand Down
23 changes: 19 additions & 4 deletions frontend/__tests__/UserPages.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (dv4all)
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (dv4all) (dv4all)
// SPDX-FileCopyrightText: 2023 Ewan Cahen (Netherlands eScience Center) <[email protected]>
Expand All @@ -11,8 +12,18 @@ import {WithAppContext, mockSession} from '~/utils/jest/WithAppContext'

import UserPages from '../pages/user/[section]'

import mockSoftwareByMaintainer from '~/components/user/software/__mocks__/softwareByMaintainer.json'
import mockProjectsByMaintainer from '~/components/user/project/__mocks__/projectsByMaintainer.json'

// use DEFAULT MOCK for login providers list
// required when AppHeader component is used
jest.mock('~/auth/api/useLoginProviders')
// MOCK user agreement call
jest.mock('~/components/user/settings/fetchAgreementStatus')
// MOCK user project list
jest.mock('~/components/user/project/useUserProjects')
// MOCK user software list
jest.mock('~/components/user/software/useUserSoftware')

// MOCKS
const mockProps = {
Expand Down Expand Up @@ -73,25 +84,29 @@ describe('pages/user/[section].tsx', () => {
it('renders user software section', async() => {
mockProps.section = 'software'

const {container} = render(
render(
<WithAppContext options={{session:mockSession}}>
<UserPages {...mockProps} />
</WithAppContext>
)

const loader = screen.getByRole('progressbar')
// validate software cards are shown
const software = screen.getAllByTestId('software-card-link')
expect(software.length).toEqual(mockSoftwareByMaintainer.length)
})

it('renders user projects section', async() => {
mockProps.section = 'projects'

const {container} = render(
render(
<WithAppContext options={{session:mockSession}}>
<UserPages {...mockProps} />
</WithAppContext>
)

const loader = screen.getByRole('progressbar')
// validate project cards are shown
const project = screen.getAllByTestId('project-card-link')
expect(project.length).toEqual(mockProjectsByMaintainer.length)
})

})
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-FileCopyrightText: 2022 Dusan Mijatovic (dv4all)
// SPDX-FileCopyrightText: 2022 dv4all
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

Expand Down
95 changes: 56 additions & 39 deletions frontend/auth/RsdAdminContent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SPDX-FileCopyrightText: 2022 Dusan Mijatovic (dv4all)
// SPDX-FileCopyrightText: 2022 dv4all
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

import {render, screen} from '@testing-library/react'
import {WrappedComponentWithProps} from '~/utils/jest/WrappedComponents'
import {useAuth, defaultSession} from '.'
import {WithAppContext, mockSession} from '~/utils/jest/WithAppContext'
import {useAuth,Session} from './index'

import RsdAdminContent from './RsdAdminContent'

Expand All @@ -26,53 +28,68 @@ function ProtectedComponent() {


it('Shows loader when session.status==="loading"', () => {
defaultSession.status='loading'
render(WrappedComponentWithProps(ProtectedComponent, {
session: defaultSession
}))
const loader = screen.getByRole('progressbar')
expect(loader).toBeInTheDocument()
const testSession = {
...mockSession,
status: 'loading'
} as Session

render(
<WithAppContext options={{session: testSession}}>
<ProtectedComponent />
</WithAppContext>
)

screen.getByRole('progressbar')
// screen.debug()
})

it('Protects content with 401 when session.status==="missing"', () => {
defaultSession.status='missing'
render(WrappedComponentWithProps(ProtectedComponent, {
session: defaultSession
}))
const heading = screen.getByRole('heading')
expect(heading.innerHTML).toContain('401')
const testSession = {
...mockSession,
status: 'missing'
} as Session

render(
<WithAppContext options={{session: testSession}}>
<ProtectedComponent />
</WithAppContext>
)

screen.getByRole('heading', {name:'401'})

})

it('Protects content with 403 when authenticated but not rsd_admin', () => {
defaultSession.status = 'authenticated'
defaultSession.user = {
iss: 'rsd_auth',
role: 'rsd_user',
exp: 1212121212,
account: 'test-account-string',
name: 'John Doe'
}
render(WrappedComponentWithProps(ProtectedComponent, {
session: defaultSession
}))
const heading = screen.getByRole('heading')
expect(heading.innerHTML).toContain('403')
const testSession = {
...mockSession
} as Session

render(
<WithAppContext options={{session: testSession}}>
<ProtectedComponent />
</WithAppContext>
)
screen.getByRole('heading', {name: '403'})
// screen.debug()
})

it('Shows content when authenticated AND rsd_admin', () => {
defaultSession.status = 'authenticated'
defaultSession.user = {
iss: 'rsd_auth',
role: 'rsd_admin',
exp: 1212121212,
account: 'test-account-string',
name: 'John Doe'
}
defaultSession.token='TEST_RANDOM_TOKEN'
render(WrappedComponentWithProps(ProtectedComponent, {
session: defaultSession
}))

const testSession = {
...mockSession,
user: {
...mockSession.user,
role: 'rsd_admin'
}

} as Session

render(
<WithAppContext options={{session: testSession}}>
<ProtectedComponent />
</WithAppContext>
)

const heading = screen.getByRole('heading')
expect(heading.innerHTML).toContain(adminText)
// screen.debug()
Expand Down
2 changes: 0 additions & 2 deletions frontend/auth/RsdAdminContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import PageErrorMessage from '~/components/layout/PageErrorMessage'

export default function RsdAdminContent({children}:{children:any}) {
const {session} = useAuth()
// if slug is provided we need to make api call to check if user
// is maintainer of the software
const [status, setStatus] = useState(session?.status ? session?.status :'loading')

useEffect(() => {
Expand Down
12 changes: 12 additions & 0 deletions frontend/auth/__mocks__/refreshSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

import {Session} from '..'
import {mockSession} from '~/utils/jest/WithAppContext'

export async function refreshSession(): Promise<Session | null> {
// return default mocked session
return mockSession
}
Loading

0 comments on commit 6f49303

Please sign in to comment.