From fe88bc42bda1034de7b9aa6b53c385b8a9d387ea Mon Sep 17 00:00:00 2001 From: Mika Kattainen Date: Tue, 18 May 2021 11:00:18 +0300 Subject: [PATCH] Fix tests to work when untranslated items are hidden --- src/components/AgeGroupItem/index.js | 3 +- src/components/AgeGroupItem/index.test.js | 37 +++++++++++++++++++++- src/components/TaskGroupItem/index.test.js | 34 ++++++++++++++------ src/helpers/index.js | 5 ++- src/views/AgeGroups/index.js | 1 + 5 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/components/AgeGroupItem/index.js b/src/components/AgeGroupItem/index.js index 74011f1..b659a3b 100644 --- a/src/components/AgeGroupItem/index.js +++ b/src/components/AgeGroupItem/index.js @@ -8,7 +8,6 @@ import { ageGroupHasTranslatedTaskGroups, } from 'helpers' import { getAgeGroupIcon } from 'graphics/ageGroups' -import { useSelector } from 'react-redux' const StyledAgeGroupItem = styled.div` width: 50vw; @@ -54,12 +53,12 @@ const Status = styled.div` const AgeGroupItem = ({ ageGroup, + itemsByGuid, language, translations, user, userTasks, }) => { - const itemsByGuid = useSelector(state => state.itemsByGuid) const hasTranslatedTaskGroups = ageGroupHasTranslatedTaskGroups( ageGroup.item, itemsByGuid, diff --git a/src/components/AgeGroupItem/index.test.js b/src/components/AgeGroupItem/index.test.js index f12aa59..3b95575 100644 --- a/src/components/AgeGroupItem/index.test.js +++ b/src/components/AgeGroupItem/index.test.js @@ -5,6 +5,29 @@ import { renderWithTheme } from 'test' import AgeGroupItem from './index' +const testTask = { + guid: '4', + languages: [ + { + lang: 'sv', + title: 'Test task', + }, + ], +} + +const testTaskGroup = { + item: { + guid: '1', + languages: [ + { + lang: 'sv', + title: 'Test task group', + }, + ], + tasks: [testTask], + }, +} + const testAgeGroup = { ageGroupGuid: '053fa231362e95cb211c5eb85c3cbedb', guid: '053fa231362e95cb211c5eb85c3cbedb', @@ -21,15 +44,27 @@ const testAgeGroup = { minAge: '7', order: 0, title: 'Sudenpennut (7-9 v.)', + taskgroups: [{ guid: '1' }, { guid: '2' }, { guid: '3' }], }, } +const itemsByGuid = { + '1': testTaskGroup, + '2': { guid: '2' }, + '3': { guid: '3' }, +} + describe('AgeGroupItem component', () => { it('displays the title of the given age group with the given language', () => { const language = 'sv' const { getByTestId } = renderWithTheme( - + ) const elem = getByTestId('title') diff --git a/src/components/TaskGroupItem/index.test.js b/src/components/TaskGroupItem/index.test.js index 12747a7..4537b38 100644 --- a/src/components/TaskGroupItem/index.test.js +++ b/src/components/TaskGroupItem/index.test.js @@ -1,14 +1,27 @@ import { getSubTaskGroupsOrTasksText } from './index' +const language = 'fi' + +const taskGroup = { + guid: '1234', + tasks: [{ guid: '1' }, { guid: '2' }, { guid: '3' }], + taskgroups: [{ guid: '4' }, { guid: '5' }, { guid: '6' }], + languages: [{ lang: language, title: 'Sudenpennut' }], +} + +const translated = { + tasks: [{ guid: '1' }, { guid: '2' }], + subGroups: [{ guid: '4' }, { guid: '5' }], +} + +const itemsByGuid = { + '1234': taskGroup, +} + const taskGroupItemProps = { - taskGroup: { - guid: '1234', - tasks: [{ guid: '1' }, { guid: '2' }, { guid: '3' }], - taskgroups: [{ guid: '4' }, { guid: '5' }, { guid: '6' }], - languages: [{ lang: 'fi', title: 'Sudenpennut' }], - }, + taskGroup, ageGroupIndex: 2, - language: 'sv', + language, tasksTerm: 'askelta', } @@ -16,10 +29,13 @@ describe('TaskGroupItem component', () => { it('renders the amounts of given subTaskGroups and tasks', () => { const text = getSubTaskGroupsOrTasksText( taskGroupItemProps.tasksTerm, - taskGroupItemProps.taskGroup + taskGroupItemProps.taskGroup, + language, + itemsByGuid, + translated ) expect(text).toBe( - `${taskGroupItemProps.taskGroup.tasks.length} + ${taskGroupItemProps.taskGroup.taskgroups.length} ${taskGroupItemProps.tasksTerm}` + `${translated.tasks.length} + ${translated.subGroups.length} ${taskGroupItemProps.tasksTerm}` ) }) }) diff --git a/src/helpers/index.js b/src/helpers/index.js index 0ed3074..9bd8ead 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -137,7 +137,10 @@ export const getAgeGroupTasks = ageGroup => { */ export const getTranslatedTaskGroups = (taskGroups, itemsByGuid, language) => { return taskGroups.reduce((acc, taskGroupGuid) => { - const taskGroup = itemsByGuid[taskGroupGuid.guid].item + const taskGroup = + itemsByGuid[taskGroupGuid.guid] && itemsByGuid[taskGroupGuid.guid].item + if (!taskGroup) return acc + const taskGroupTranslations = taskGroup.languages.find( group => group.lang === language ) diff --git a/src/views/AgeGroups/index.js b/src/views/AgeGroups/index.js index 781d55c..ff7d78e 100644 --- a/src/views/AgeGroups/index.js +++ b/src/views/AgeGroups/index.js @@ -146,6 +146,7 @@ const AgeGroups = ({ theme }) => {