From c385b1e93f335b813501ad049db703830381a1ef Mon Sep 17 00:00:00 2001 From: Akiomi Kamakura Date: Sat, 13 Nov 2021 11:09:41 +0900 Subject: [PATCH] Fix mock of DailyCache --- src/__mocks__/api/v3/client.ts | 16 +++++++-------- src/__mocks__/services/daily-cache.ts | 29 +++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/__mocks__/api/v3/client.ts b/src/__mocks__/api/v3/client.ts index 7481ffc..b539237 100644 --- a/src/__mocks__/api/v3/client.ts +++ b/src/__mocks__/api/v3/client.ts @@ -1,6 +1,7 @@ import { default as company } from '~/__mocks__/fixtures/v3/company.js' import { default as daily } from '~/__mocks__/fixtures/v3/daily.js' import { default as quarter } from '~/__mocks__/fixtures/v3/quarter.js' +import { Daily } from '~/entities/v3/daily' export class BuffettCodeApiClientV3 { public mockCompany = jest.fn() @@ -13,9 +14,8 @@ export class BuffettCodeApiClientV3 { this.mockQuarter.mockReturnValue(quarter) } - company(): object | null { - const company = this.mockCompany()['data'] - return company ? company : null + company(): object { + return this.mockCompany()['data'] } quarter(): object | null { @@ -23,14 +23,12 @@ export class BuffettCodeApiClientV3 { return quarter ? quarter : null } - daily(): object | null { - const daily = this.mockDaily()['data'] - return daily ? daily : null + daily(): Daily { + return Daily.fromResponse(this.mockDaily()) } - ondemandDaily(): object | null { - const daily = this.mockDaily()['data'] - return daily ? daily : null + ondemandDaily(): Daily { + return Daily.fromResponse(this.mockDaily()) } ondemandQuarter(): object | null { diff --git a/src/__mocks__/services/daily-cache.ts b/src/__mocks__/services/daily-cache.ts index e4922a3..2b5b0c2 100644 --- a/src/__mocks__/services/daily-cache.ts +++ b/src/__mocks__/services/daily-cache.ts @@ -1,9 +1,20 @@ +import { Daily } from '~/entities/v3/daily' import { DateParam } from '~/fiscal-periods/date-param' export class DailyCache { static readonly cache = {} - static get(ticker: string, date: Date | DateParam): object | null { + static get(ticker: string, date: Date | DateParam): Daily | null { + const cachedData = this.getData(ticker, date) + const cachedColumnDescription = this.getColumnDescription() + if (cachedData == undefined || cachedColumnDescription == undefined) { + return null + } + + return new Daily(cachedData, cachedColumnDescription) + } + + static getData(ticker: string, date: Date | DateParam): object | null { if (date instanceof Date) { date = new DateParam(date) } @@ -12,10 +23,24 @@ export class DailyCache { return cached === undefined ? null : cached } - static put(ticker: string, daily: object): void { + static getColumnDescription(): object | null { + const cached = this.cache['column-description'] + return cached === undefined ? null : cached + } + + static put(ticker: string, daily: Daily): void { + this.putData(ticker, daily.data) + this.putColumnDescription(daily.columnDescription) + } + + static putData(ticker: string, daily: object): void { DailyCache.cache[`${ticker}-${daily['day']}`] = daily } + static putColumnDescription(columnDescription: object): void { + this.cache['column-description'] = columnDescription + } + // for testing static clearAll(): void { Object.keys(this.cache).forEach(key => delete this.cache[key])