Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Oct 11, 2023
1 parent ebec718 commit cc8de37
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/components/sectionList/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { IdentifiableObject } from '../../types/generated'

export type SelectedColumn = {
label: string
path: string
Expand Down
29 changes: 20 additions & 9 deletions src/pages/dataElements/List.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FetchError } from '@dhis2/app-runtime'
import {
render,
waitForElementToBeRemoved,
Expand All @@ -9,6 +10,7 @@ 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'

Check warning on line 13 in src/pages/dataElements/List.spec.tsx

View workflow job for this annotation

GitHub Actions / lint

'useModelListView' is defined but never used
import { SECTIONS_MAP } from '../../lib'
import { useSchemaStore } from '../../lib/schemas/schemaStore'
import { ModelSchemas } from '../../lib/useLoadApp'
Expand Down Expand Up @@ -37,10 +39,19 @@ const renderSection = async (customData: CustomData) => {
return result
}

// userDataStore returns 404 if user hasnt edited a view, this is expected behaviour
const error404 = new FetchError({
type: 'unknown',
message: '404 not found',
details: { httpStatusCode: 404 } as FetchError['details'],
})
const defaultUserDataStoreData = () => Promise.reject(new FetchError(error404))

describe('Data Elements List', () => {
const originalWarn = console.warn
jest.spyOn(console, 'warn').mockImplementation((value) => {
if (!value.match(/No server timezone/)) {
console.warn(value)
originalWarn(value)
}
})

Expand All @@ -53,7 +64,7 @@ describe('Data Elements List', () => {
it('should show the list of elements', async () => {
const customData = {
'dataElements/gist': dataElementsMock,
userDataStore: {},
userDataStore: defaultUserDataStoreData,
}
const { getByText, getByTestId } = await renderSection(customData)

Expand All @@ -70,7 +81,7 @@ describe('Data Elements List', () => {
it('should display all the columns', async () => {
const customData = {
'dataElements/gist': dataElementsMock,
userDataStore: {},
userDataStore: defaultUserDataStoreData,
}
const { getByText } = await renderSection(customData)
const columns = [
Expand All @@ -87,7 +98,7 @@ describe('Data Elements List', () => {
})
it('should allow searching for value', async () => {
const customData = {
userDataStore: {},
userDataStore: defaultUserDataStoreData,
'dataElements/gist': (
resource: string,
r: { params: { filter: string[] } }
Expand Down Expand Up @@ -118,7 +129,7 @@ describe('Data Elements List', () => {

it('should display error when an API call fails', async () => {
const customData = {
userDataStore: {},
userDataStore: defaultUserDataStoreData,
'dataElements/gist': () => {
return Promise.reject('401 backend error')
},
Expand All @@ -136,7 +147,7 @@ describe('Data Elements List', () => {

const renderWithPager = async () => {
const customData = {
userDataStore: {},
userDataStore: defaultUserDataStoreData,
'dataElements/gist': (
resource: string,
r: { params: { filter: string[]; page: number } }
Expand Down Expand Up @@ -240,7 +251,7 @@ describe('Data Elements List', () => {
// I tried different approaches and failed. Leaving it here temporarily in case someone want to give it a go.
it.skip('should not show next in last page', async () => {
const { getByTestId, findByText } = await renderSection({
userDataStore: {},
userDataStore: defaultUserDataStoreData,
'dataElements/gist': {
pager: {
page: 54,
Expand Down Expand Up @@ -275,7 +286,7 @@ describe('Data Elements List', () => {
// select all
it('should allow selecting all items', async () => {
const customData = {
userDataStore: {},
userDataStore: defaultUserDataStoreData,
'dataElements/gist': dataElementsMock,
}
const { getByTestId, queryAllByTestId } = await renderSection(
Expand All @@ -298,7 +309,7 @@ describe('Data Elements List', () => {
// empty list
it('should allow selecting all items', async () => {
const customData = {
userDataStore: {},
userDataStore: defaultUserDataStoreData,
'dataElements/gist': { ...dataElementsMock, result: [] },
}
const { getByTestId } = await renderSection(customData)
Expand Down
1 change: 1 addition & 0 deletions src/pages/dataElements/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const Component = () => {
// refetched on mount by effect below
{ lazy: true }
)

useEffect(() => {
// wait to fetch until selected-columns are loaded
// so we dont fetch data multiple times
Expand Down

0 comments on commit cc8de37

Please sign in to comment.