-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
502 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import Promise from 'bluebird' | ||
import KingsRepository from 'chronology/infrastructure/KingsRepository' | ||
import { King } from 'chronology/ui/BrinkmanKings/BrinkmanKings' | ||
|
||
export interface kingsSearch { | ||
fetchAll(query: string): Promise<readonly King[]> | ||
} | ||
|
||
export default class KingsService implements kingsSearch { | ||
private readonly kingsRepository: KingsRepository | ||
private _kings: King[] = [] | ||
kingOptions: Array<{ label: string; value: King }> = [] | ||
|
||
constructor(kingsRepository: KingsRepository) { | ||
this.kingsRepository = kingsRepository | ||
} | ||
|
||
static async createAndInitialize( | ||
kingsRepository: KingsRepository | ||
): Promise<KingsService> { | ||
const service = new KingsService(kingsRepository) | ||
await service.initializeKings() | ||
return service | ||
} | ||
|
||
get kings(): King[] { | ||
return this._kings | ||
} | ||
|
||
private async initializeKings(): Promise<void> { | ||
try { | ||
this._kings = await this.fetchAll() | ||
this.kingOptions = this.getKingOptions() | ||
} catch (error) { | ||
console.error('Failed to initialize kingsCollection', error) | ||
} | ||
} | ||
|
||
fetchAll(): Promise<King[]> { | ||
return this.kingsRepository.fetchAll() | ||
} | ||
|
||
private getKingOptions(): Array<{ label: string; value: King }> { | ||
return this.kings | ||
.filter((king) => !['16', '17'].includes(king.dynastyNumber)) | ||
.map((king) => { | ||
return { | ||
label: this.getKingSelectLabel(king), | ||
value: king, | ||
} | ||
}) | ||
} | ||
|
||
private getKingSelectLabel(king: King): string { | ||
const kingYears = king.date ? ` (${king.date})` : '' | ||
return `${king.name}${kingYears}, ${king.dynastyName}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { King } from 'chronology/ui/BrinkmanKings/BrinkmanKings' | ||
import Promise from 'bluebird' | ||
import ApiClient from 'http/ApiClient' | ||
|
||
export default class KingsRepository { | ||
private readonly apiClient: ApiClient | ||
|
||
constructor(apiClient: ApiClient) { | ||
this.apiClient = apiClient | ||
} | ||
|
||
fetchAll(): Promise<King[]> { | ||
return this.apiClient.fetchJson('/kings-all', false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import BrinkmanKingsTable from 'chronology/ui/BrinkmanKings/BrinkmanKings' | ||
|
||
/* | ||
jest.doMock('chronology/domain/BrinkmanKings.json', () => ({ | ||
__esModule: true, | ||
default: [], | ||
})) | ||
*/ | ||
jest.mock('chronology/domain/BrinkmanKings.json', () => [], { virtual: true }) | ||
|
||
test('Snapshot', () => { | ||
expect(BrinkmanKingsTable()).toMatchSnapshot() | ||
}) | ||
|
||
test('Displays only kings from Brinkman in table', () => { | ||
render(BrinkmanKingsTable()) | ||
expect(screen.getByText('Maništušu')).not.toBeInTheDocument() | ||
}) |
Oops, something went wrong.