Skip to content

Commit

Permalink
Improve & update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Aug 1, 2023
1 parent d5d212a commit 36b15ad
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 100 deletions.
33 changes: 19 additions & 14 deletions src/fragmentarium/domain/Date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,39 @@ export enum Ur3Calendar {
}

export class MesopotamianDate {
era: King | string | undefined
year: DateField
month: MonthField
day: DateField
king?: King
isSeleucidEra?: boolean
ur3Calendar?: Ur3Calendar

constructor(
era: King | string | undefined,
year: DateField,
month: MonthField,
day: DateField,
king?: King,
isSeleucidEra?: boolean,
ur3Calendar?: Ur3Calendar
) {
this.era = era
this.year = year
this.month = month
this.day = day
this.king = king
this.isSeleucidEra = isSeleucidEra
this.ur3Calendar = ur3Calendar
}

static fromJson(dateJSON: MesopotamianDateDto): MesopotamianDate {
const { era, year, month, day } = dateJSON
return new MesopotamianDate(era, year, month, day)
const { year, month, day, king, isSeleucidEra, ur3Calendar } = dateJSON
return new MesopotamianDate(
year,
month,
day,
king,
isSeleucidEra,
ur3Calendar
)
}

toString(): string {
Expand All @@ -59,7 +69,7 @@ export class MesopotamianDate {
]
return `${dateParts.join(
'.'
)}${this.eraToString()}${this.ur3CalendarToString()}`
)}${this.kingOrEraToString()}${this.ur3CalendarToString()}`
}

yearToString(): string {
Expand Down Expand Up @@ -95,14 +105,9 @@ export class MesopotamianDate {
}`
}

eraToString(): string {
const era =
this.era === 'seleucid'
? 'SE'
: typeof this.era === 'string'
? this.era
: this.era?.name ?? ''
return era ? ' ' + era : era
kingOrEraToString(): string {
const eraOrKing = this.isSeleucidEra ? 'SE' : this.king?.name ?? ''
return eraOrKing ? ' ' + eraOrKing : eraOrKing
}

ur3CalendarToString(): string {
Expand Down
6 changes: 4 additions & 2 deletions src/fragmentarium/domain/FragmentDtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Introduction, Notes, ScriptDto } from './fragment'
import { RecordEntry } from './RecordEntry'
import MuseumNumber from './MuseumNumber'
import { King } from 'common/BrinkmanKings'
import { Ur3Calendar } from './Date'

interface MeasureDto {
value?: number
Expand All @@ -26,11 +27,12 @@ interface MonthFieldDto extends DateFieldDto {
}

export interface MesopotamianDateDto {
era: King | string // ToDo: ADD SCHEMA
year: DateFieldDto
month: MonthFieldDto
day: DateFieldDto
ur3Calendar?: string
king?: King
isSeleucidEra?: boolean
ur3Calendar?: Ur3Calendar
}

export interface TextDto {
Expand Down
54 changes: 31 additions & 23 deletions src/fragmentarium/ui/info/DateSelection.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { render, fireEvent, screen } from '@testing-library/react'
import { render, fireEvent, screen, act } from '@testing-library/react'
import DateSelection from './DateSelection'
import { fragment as mockFragment } from 'test-support/test-fragment'
import SessionContext from 'auth/SessionContext'
Expand Down Expand Up @@ -34,78 +34,86 @@ describe('DateSelection', () => {
)

const editButton = screen.getByLabelText('Browse dates button')
fireEvent.click(editButton)
act(() => {
fireEvent.click(editButton)
})

const popover = screen.getByRole('tooltip')
expect(popover).toBeInTheDocument()
})

test('hides the date popover when the Save button is clicked', () => {
test('hides the date popover when the Save button is clicked', async () => {
render(
<SessionContext.Provider value={session}>
<DateSelection fragment={mockFragment} updateDate={mockUpdateDate} />
</SessionContext.Provider>
)

const editButton = screen.getByLabelText('Browse dates button')
fireEvent.click(editButton)
act(() => {
fireEvent.click(editButton)
})

const saveButton = screen.getByText('Save')
fireEvent.click(saveButton)
await act(async () => {
fireEvent.click(saveButton)
})

const popover = screen.queryByTestId('popover-select-date')
expect(popover).not.toBeInTheDocument()
})

test('calls the updateDate function with the selected date values when Save is clicked', () => {
test('calls the updateDate function with the selected date values when Save is clicked', async () => {
render(
<SessionContext.Provider value={session}>
<DateSelection fragment={mockFragment} updateDate={mockUpdateDate} />
</SessionContext.Provider>
)

const editButton = screen.getByLabelText('Browse dates button')
fireEvent.click(editButton)

act(() => {
fireEvent.click(editButton)
})
const yearInput = screen.getByPlaceholderText('Year')
fireEvent.change(yearInput, { target: { value: '2022' } })

const monthInput = screen.getByPlaceholderText('Month')
fireEvent.change(monthInput, { target: { value: '3' } })

const dayInput = screen.getByPlaceholderText('Day')
fireEvent.change(dayInput, { target: { value: '15' } })

const saveButton = screen.getByText('Save')
fireEvent.click(saveButton)

await act(async () => {
fireEvent.change(yearInput, { target: { value: '2022' } })
fireEvent.change(monthInput, { target: { value: '3' } })
fireEvent.change(dayInput, { target: { value: '15' } })
})
await act(async () => {
fireEvent.click(saveButton)
})
expect(mockUpdateDate).toHaveBeenCalledTimes(1)
expect(mockUpdateDate).toHaveBeenCalledWith({
day: { broken: false, uncertain: false, value: '15' },
era: 'seleucid',
month: {
broken: false,
intercalary: false,
uncertain: false,
value: '3',
},
ur3Calendar: undefined,
year: { broken: false, uncertain: false, value: '2022' },
king: undefined,
isSeleucidEra: true,
ur3Calendar: undefined,
})
})

test('displays the loading spinner when saving', () => {
test('displays the loading spinner when saving', async () => {
render(
<SessionContext.Provider value={session}>
<DateSelection fragment={mockFragment} updateDate={mockUpdateDate} />
</SessionContext.Provider>
)
const editButton = screen.getByLabelText('Browse dates button')
fireEvent.click(editButton)

act(() => {
fireEvent.click(editButton)
})
const saveButton = screen.getByText('Save')
fireEvent.click(saveButton)

fireEvent.click(saveButton)
const loadingSpinner = screen.getByText('Saving...')
expect(loadingSpinner).toBeInTheDocument()
})
Expand Down
Loading

0 comments on commit 36b15ad

Please sign in to comment.