Skip to content

Commit

Permalink
Add tests & update
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Aug 7, 2023
1 parent e505fd6 commit e41636e
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 72 deletions.
64 changes: 0 additions & 64 deletions src/fragmentarium/domain/Date.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//import produce, { castDraft, Draft, immerable } from 'immer'
//import { immerable } from 'immer'
//import _ from 'lodash'
import { King } from 'common/BrinkmanKings'
import { MesopotamianDateDto } from 'fragmentarium/domain/FragmentDtos'
import { romanize } from 'romans'
Expand Down Expand Up @@ -119,64 +116,3 @@ export class MesopotamianDate {
return ''
}
}

/*
export class Dates {
readonly dates: ReadonlyArray<Date>
constructor(dates: Date[]) {
this.dates = dates
}
static fromJson(datesJSON: readonly MesopotamianDateDto[]): Dates {
return new Dates(datesJSON.map((dateJson) => Date.fromJson(dateJson)))
}
isPresent(date: Date): boolean {
return this.dates.some(
(element) =>
JSON.stringify(element.category) === JSON.stringify(date.category)
)
}
find(date: Date): Date | undefined {
return _.find(
this.dates,
(elem) => JSON.stringify(elem.category) === JSON.stringify(date.category)
)
}
insertWithOrder(date: Date, indexLookup: readonly string[][]): Dates {
return produce(this, (draft: Draft<Dates>) => {
draft.dates = castDraft(
_.sortBy([...this.dates, date], (date) =>
JSON.stringify(indexLookup).indexOf(JSON.stringify(date.category))
)
)
})
}
delete(date: Date): Dates {
return produce(this, (draft: Draft<Dates>) => {
draft.dates = castDraft(
this.dates.filter(
(elem) => JSON.stringify(elem) !== JSON.stringify(date)
)
)
})
}
replace(date: Date): Dates {
return produce(this, (draft: Draft<Dates>) => {
const dates = _.cloneDeep(this.dates as Date[])
const index = _.findIndex(
this.dates,
(content) =>
JSON.stringify(content.category) === JSON.stringify(date.category)
)
dates.splice(index, 1, date)
draft.dates = castDraft(dates)
})
}
*/
5 changes: 3 additions & 2 deletions src/fragmentarium/ui/info/DateSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default function DateSelection({
date?.isSeleucidEra ?? false
)
const [isCalendarFieldDisplayed, setIsCalenderFieldDisplayed] = useState(
false
date?.ur3Calendar ? true : false
)
const [king, setKing] = useState<King | undefined>(date?.king)
const [ur3Calendar, setUr3Calendar] = useState<Ur3Calendar | undefined>(
undefined
date?.ur3Calendar ?? undefined
)
const [yearValue, setYearValue] = useState(date?.year.value ?? '')
const [yearBroken, setYearBroken] = useState(date?.year.isBroken ?? false)
Expand Down Expand Up @@ -91,6 +91,7 @@ export default function DateSelection({
date,
isSeleucidEra,
isCalendarFieldDisplayed,
ur3Calendar,
setKing,
setIsSeleucidEra,
setIsCalenderFieldDisplayed,
Expand Down
110 changes: 110 additions & 0 deletions src/fragmentarium/ui/info/DateSelectionInput.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { render, screen, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
import selectEvent from 'react-select-event'
import userEvent from '@testing-library/user-event'
import {
getKingInput,
getUr3CalendarField,
getDateInputGroups,
} from './DateSelectionInput'
import { mesopotamianDateFactory } from 'test-support/date-fixtures'
import { Ur3Calendar } from 'fragmentarium/domain/Date'

describe('King Input', () => {
it('Renders and handels the Seleucid Era switch', () => {
const setIsSeleucidEra = jest.fn()
render(
getKingInput({
date: mesopotamianDateFactory.build(),
isSeleucidEra: false,
isCalendarFieldDisplayed: false,
ur3Calendar: undefined,
setKing: jest.fn(),
setIsSeleucidEra,
setIsCalenderFieldDisplayed: jest.fn(),
setUr3Calendar: jest.fn(),
})
)
const switchElem = screen.getByLabelText('Seleucid Era')
expect(switchElem).toBeInTheDocument()
switchElem.click()
expect(setIsSeleucidEra).toHaveBeenCalledWith(true)
})
})

describe('Ur3 Calendar Field', () => {
it('Renders and handles the Ur3 Calendar field', async () => {
const setUr3Calendar = jest.fn()

render(
getUr3CalendarField({
date: mesopotamianDateFactory.build({ ur3Calendar: Ur3Calendar.UMMA }),
isSeleucidEra: false,
isCalendarFieldDisplayed: true,
ur3Calendar: Ur3Calendar.UMMA,
setKing: jest.fn(),
setUr3Calendar,
setIsSeleucidEra: jest.fn(),
setIsCalenderFieldDisplayed: jest.fn(),
})
)
const selectElem = screen.getByLabelText('select-calendar')
expect(selectElem).toBeInTheDocument()
const option = screen.getByText('Umma')
expect(option).toBeInTheDocument()

userEvent.type(screen.getByLabelText('select-calendar'), 'Umma')
await selectEvent.select(screen.getByLabelText('select-calendar'), 'Umma')
await waitFor(() => expect(setUr3Calendar).toHaveBeenCalledWith('Umma'))
})
})

describe('Date Input Groups', () => {
it('Renders year input group', () => {
render(
getDateInputGroups({
yearValue: '',
yearBroken: false,
yearUncertain: false,
monthValue: '',
monthBroken: false,
monthUncertain: false,
dayValue: '',
dayBroken: false,
dayUncertain: false,
setYearValue: jest.fn(),
setYearBroken: jest.fn(),
setYearUncertain: jest.fn(),
setMonthValue: jest.fn(),
setMonthBroken: jest.fn(),
setMonthUncertain: jest.fn(),
setDayValue: jest.fn(),
setDayBroken: jest.fn(),
setDayUncertain: jest.fn(),
})
)
const yearInput = screen.getByLabelText('Year')
const yearBrokenSwitch = screen.getByLabelText('Year-Broken')
const yearUncertainSwitch = screen.getByLabelText('Year-Uncertain')
const monthInput = screen.getByLabelText('Month')
const monthIntercalaryCheckbox = screen.getByLabelText('Intercalary')
const monthBrokenSwitch = screen.getByLabelText('Month-Broken')
const monthUncertainSwitch = screen.getByLabelText('Month-Uncertain')
const dayInput = screen.getByLabelText('Day')
const dayBrokenSwitch = screen.getByLabelText('Day-Broken')
const dayUncertainSwitch = screen.getByLabelText('Day-Uncertain')

expect(yearInput).toBeInTheDocument()
expect(yearBrokenSwitch).toBeInTheDocument()
expect(yearUncertainSwitch).toBeInTheDocument()

expect(monthInput).toBeInTheDocument()
expect(monthIntercalaryCheckbox).toBeInTheDocument()
expect(monthBrokenSwitch).toBeInTheDocument()
expect(monthUncertainSwitch).toBeInTheDocument()

expect(dayInput).toBeInTheDocument()
expect(dayBrokenSwitch).toBeInTheDocument()
expect(dayUncertainSwitch).toBeInTheDocument()
})
})
17 changes: 12 additions & 5 deletions src/fragmentarium/ui/info/DateSelectionInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type KingInputProps = {
date: MesopotamianDate | undefined
isSeleucidEra: boolean
isCalendarFieldDisplayed: boolean
ur3Calendar: Ur3Calendar | undefined
setKing: React.Dispatch<React.SetStateAction<King | undefined>>
setIsSeleucidEra: React.Dispatch<React.SetStateAction<boolean>>
setIsCalenderFieldDisplayed: React.Dispatch<React.SetStateAction<boolean>>
Expand Down Expand Up @@ -133,20 +134,26 @@ function getCurrentOption(
}

export function getUr3CalendarField({
ur3Calendar,
setUr3Calendar,
}: KingInputProps): JSX.Element {
const options = Object.keys(Ur3Calendar).map((key) => {
return { label: Ur3Calendar[key], value: key }
})
const value = options.find(
(option) => option.label === ur3Calendar?.toString()
)
return (
<Select
aria-label="select-calendar"
options={Object.keys(Ur3Calendar).map((key) => {
return { label: Ur3Calendar[key], value: key }
})}
options={options}
onChange={(option): void => {
setUr3Calendar(Ur3Calendar[option?.value as keyof typeof Ur3Calendar])
}}
isSearchable={true}
autoFocus={true}
placeholder="Calendar"
value={value}
/>
)
}
Expand Down Expand Up @@ -181,14 +188,14 @@ function getDateInputGroup({
/>
)}
<Form.Switch
label="Broken"
label={`${_.startCase(name)}-Broken`}
id={`${name}_broken`}
style={{ marginLeft: '10px' }}
onChange={(event) => setBroken(event.target.checked)}
checked={isBroken}
/>
<Form.Switch
label="Uncertain"
label={`${_.startCase(name)}-Uncertain`}
id={`${name}_uncertain`}
style={{ marginLeft: '10px' }}
onChange={(event) => setUncertain(event.target.checked)}
Expand Down
38 changes: 38 additions & 0 deletions src/test-support/date-fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Factory } from 'fishery'
import { MesopotamianDate, Ur3Calendar } from 'fragmentarium/domain/Date'
import Chance from 'chance'
import BrinkmanKings from 'common/BrinkmanKings.json'

const chance = new Chance()

export const mesopotamianDateFactory = Factory.define<MesopotamianDate>(() => {
const isSeleucidEra = chance.bool()
const king = isSeleucidEra ? undefined : chance.pickone(BrinkmanKings)
const year = {
value: chance.integer({ min: 1, max: 25 }).toString(),
isBroken: chance.bool(),
isUncertain: chance.bool(),
}
const ur3Calendar =
king && king.dynastyNumber === '2'
? chance.pickone(Object.values(Ur3Calendar))
: undefined

return new MesopotamianDate(
year,
{
value: chance.integer({ min: 1, max: 12 }).toString(),
isBroken: chance.bool(),
isUncertain: chance.bool(),
isIntercalary: chance.bool(),
},
{
value: chance.integer({ min: 1, max: 29 }).toString(),
isBroken: chance.bool(),
isUncertain: chance.bool(),
},
king,
isSeleucidEra,
ur3Calendar
)
})
3 changes: 2 additions & 1 deletion src/test-support/fragment-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Text, createText } from 'corpus/domain/text'
import { periodModifiers, periods } from 'common/period'
import { ExternalNumbers } from 'fragmentarium/domain/FragmentDtos'
import { MesopotamianDate } from 'fragmentarium/domain/Date'
import { mesopotamianDateFactory } from './date-fixtures'

const defaultChance = new Chance()

Expand Down Expand Up @@ -238,7 +239,7 @@ export const fragmentInfoFactory = Factory.define<FragmentInfo>(
script: scriptFactory.build(),
matchingLines: null,
editor: defaultChance.last(),
date: date(),
date: mesopotamianDateFactory.build(),
// eslint-disable-next-line camelcase
edition_date: date(),
references: associations.references ?? [],
Expand Down

0 comments on commit e41636e

Please sign in to comment.