Skip to content

Commit

Permalink
fix(test): fix list tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Nov 27, 2023
1 parent af5b5d6 commit 22ff36d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/components/sectionList/modelValue/ModelReference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const isModelReference = (value: unknown): value is ModelReference => {
}

export const ModelReference = ({ value }: { value?: ModelReference }) => {
let displayValue = value?.displayName || value?.name
let displayValue = value?.displayName
// default categoryCombos should display as None
if (displayValue === 'default') {
displayValue = i18n.t('None')
Expand Down
72 changes: 34 additions & 38 deletions src/pages/dataElements/List.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import {
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React from 'react'
import dataElementsMock from '../../__mocks__/gists/dataElementsMock.json'
import filteredDataElementsMock from '../../__mocks__/gists/filteredDataElementsMock.json'
import dataElementSchemaMock from '../../__mocks__/schema/dataElementsSchema.json'
import { useModelListView } from '../../components/sectionList/listView'
// import { useModelListView } from '../../components/sectionList/listView'
import { SECTIONS_MAP } from '../../lib'
import { useSchemaStore } from '../../lib/schemas/schemaStore'
import { ModelSchemas } from '../../lib/useLoadApp'
import TestComponentWithRouter, {
CustomData,
} from '../../testUtils/TestComponentWithRouter'
import dataElementsMock from './__mocks__/list/dataElementsMock.json'

Check failure on line 18 in src/pages/dataElements/List.spec.tsx

View workflow job for this annotation

GitHub Actions / lint

Unable to resolve path to module './__mocks__/list/dataElementsMock.json'
import filteredDataElementsMock from './__mocks__/list/filteredDataElementsMock.json'

Check failure on line 19 in src/pages/dataElements/List.spec.tsx

View workflow job for this annotation

GitHub Actions / lint

Unable to resolve path to module './__mocks__/list/filteredDataElementsMock.json'
import { Component as DataElementList } from './List'

const FIRST_DATA_ELEMENT = dataElementsMock.dataElements[0]
const FIRST_FILTERED_DATA_ELEMENT = filteredDataElementsMock.dataElements[0]

const renderSection = async (customData: CustomData) => {
const routeOptions = {
handle: { section: SECTIONS_MAP.dataElement },
Expand All @@ -33,6 +36,7 @@ const renderSection = async (customData: CustomData) => {
<DataElementList />
</TestComponentWithRouter>
)

await waitForElementToBeRemoved(() =>
result.queryByTestId('dhis2-uicore-circularloader')
)
Expand Down Expand Up @@ -63,7 +67,7 @@ describe('Data Elements List', () => {

it('should show the list of elements', async () => {
const customData = {
'dataElements/gist': dataElementsMock,
dataElements: dataElementsMock,
userDataStore: defaultUserDataStoreData,
}
const { getByText, getByTestId } = await renderSection(customData)
Expand All @@ -72,47 +76,47 @@ describe('Data Elements List', () => {
getByText('Accute Flaccid Paralysis (Deaths < 5 yrs)')
).not.toBeNull()

const { id } = dataElementsMock.result[0]
const { id } = FIRST_DATA_ELEMENT
const firstRow = getByTestId(`section-list-row-${id}`)
expect(firstRow).toHaveTextContent(
/Accute Flaccid Paralysis \(Deaths < 5 yrs\)AggregateNumber\d+ years agoPublic can edit/
)
})
it('should display all the columns', async () => {
const customData = {
'dataElements/gist': dataElementsMock,
dataElements: dataElementsMock,
userDataStore: defaultUserDataStoreData,
}
const { getByText } = await renderSection(customData)
const columns = [
'Name',
'Domain',
'Value',
'Value type',
'Last updated',
'Public access',
'Actions',
]
columns.forEach((colummn) => {
expect(getByText(colummn)).not.toBeNull()

columns.forEach((column) => {
expect(getByText(column, { selector: 'span' })).not.toBeNull()
})
})
it('should allow searching for value', async () => {
const customData = {
userDataStore: defaultUserDataStoreData,
'dataElements/gist': (
dataElements: (
resource: string,
r: { params: { filter: string[] } }
) => {
const filters = r.params.filter?.join(',')
switch (filters) {
case '':
return Promise.resolve(dataElementsMock)
case '0:name:ilike:Age of LLINs,0:code:ilike:Age of LLINs,0:shortName:ilike:Age of LLINs,0:id:eq:Age of LLINs':
return Promise.resolve(filteredDataElementsMock)
default:
return Promise.reject(
`no matched data provider for resource:${resource},filters:${filters}`
)
if (filters === '') {
return Promise.resolve(dataElementsMock)
} else if (filters.startsWith('identifiable')) {
return Promise.resolve(filteredDataElementsMock)
} else {
return Promise.reject(
`no matched data provider for resource:${resource},filters:${filters}`
)
}
},
}
Expand All @@ -130,7 +134,7 @@ describe('Data Elements List', () => {
it('should display error when an API call fails', async () => {
const customData = {
userDataStore: defaultUserDataStoreData,
'dataElements/gist': () => {
dataElements: () => {
return Promise.reject('401 backend error')
},
}
Expand All @@ -148,7 +152,7 @@ describe('Data Elements List', () => {
const renderWithPager = async () => {
const customData = {
userDataStore: defaultUserDataStoreData,
'dataElements/gist': (
dataElements: (
resource: string,
r: { params: { filter: string[]; page: number } }
) => {
Expand All @@ -159,13 +163,11 @@ describe('Data Elements List', () => {
page: 1,
pageSize: 20,
total: 1061,
nextPage:
'/dataElements/gist?pageListName=result&total=true&order=name:ASC&fields=access,id,name,code,domainType,valueType,lastUpdated,sharing&pageSize=20&page=2',
pageCount: 54,
},
result: [
dataElements: [
{
...filteredDataElementsMock.result[0],
...FIRST_FILTERED_DATA_ELEMENT,
name: 'first page result',
},
],
Expand All @@ -175,15 +177,11 @@ describe('Data Elements List', () => {
page: 2,
pageSize: 20,
total: 1061,
prevPage:
'/dataElements/gist?pageListName=result&total=true&order=name:ASC&fields=access,id,name,code,domainType,valueType,lastUpdated,sharing&pageSize=20&page=1',
nextPage:
'/dataElements/gist?pageListName=result&total=true&order=name:ASC&fields=access,id,name,code,domainType,valueType,lastUpdated,sharing&pageSize=20&page=3',
pageCount: 54,
},
result: [
dataElements: [
{
...filteredDataElementsMock.result[0],
...FIRST_FILTERED_DATA_ELEMENT,
name: 'second page result',
},
],
Expand Down Expand Up @@ -252,18 +250,16 @@ describe('Data Elements List', () => {
it.skip('should not show next in last page', async () => {
const { getByTestId, findByText } = await renderSection({
userDataStore: defaultUserDataStoreData,
'dataElements/gist': {
dataElements: {
pager: {
page: 54,
pageSize: 20,
total: 1061,
prevPage:
'/dataElements/gist?pageListName=result&total=true&order=name:ASC&fields=access,id,name,code,domainType,valueType,lastUpdated,sharing&pageSize=20&page=53',
pageCount: 54,
},
result: [
{
...filteredDataElementsMock.result[0],
...FIRST_FILTERED_DATA_ELEMENT,
name: 'last page result',
},
],
Expand All @@ -287,7 +283,7 @@ describe('Data Elements List', () => {
it('should allow selecting all items', async () => {
const customData = {
userDataStore: defaultUserDataStoreData,
'dataElements/gist': dataElementsMock,
dataElements: dataElementsMock,
}
const { getByTestId, queryAllByTestId } = await renderSection(
customData
Expand All @@ -307,10 +303,10 @@ describe('Data Elements List', () => {
})

// empty list
it('should allow selecting all items', async () => {
it('should show message when no items match filter', async () => {
const customData = {
userDataStore: defaultUserDataStoreData,
'dataElements/gist': { ...dataElementsMock, result: [] },
dataElements: { ...dataElementsMock, dataElements: [] },
}
const { getByTestId } = await renderSection(customData)

Expand Down

0 comments on commit 22ff36d

Please sign in to comment.