diff --git a/src/resources/schedule.test.ts b/src/resources/schedule.test.ts index 1e7b9dc5..92a62f1a 100644 --- a/src/resources/schedule.test.ts +++ b/src/resources/schedule.test.ts @@ -12,8 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. import * as faas from '../faas/index'; -import { schedule, RateWorkerOptions } from '.'; - +import { schedule, RateWorkerOptions, Frequency } from '.'; jest.mock('../faas/index'); describe('Schedule', () => { @@ -83,7 +82,7 @@ describe('Schedule', () => { }); }); - ['day', 'hour', 'minute'].forEach((rate) => { + ['day', 'hour', 'minute'].forEach((rate: Frequency) => { describe(`when create a new schedule with rate ${rate}`, () => { afterAll(() => { jest.resetAllMocks(); @@ -101,7 +100,7 @@ describe('Schedule', () => { const expectedOpts = new RateWorkerOptions( 'main', 1, - `${rate}s` as any + `${rate}s` as Frequency ); expect(faas.Faas).toBeCalledWith(expectedOpts); }); @@ -111,4 +110,29 @@ describe('Schedule', () => { }); }); }); + + ['days', 'hours', 'minutes'].forEach((rate: Frequency) => { + describe(`when create a new schedule with rate ${rate}`, () => { + afterAll(() => { + jest.resetAllMocks(); + }); + + beforeAll(async () => { + await schedule('main').every(`7 ${rate}`, mockFn); + }); + + it('should create a new FaasClient', () => { + expect(faas.Faas).toBeCalledTimes(1); + }); + + it('should provide Faas with ApiWorkerOptions', () => { + const expectedOpts = new RateWorkerOptions('main', 7, rate); + expect(faas.Faas).toBeCalledWith(expectedOpts); + }); + + it('should call FaasClient::start()', () => { + expect(startSpy).toBeCalledTimes(1); + }); + }); + }); }); diff --git a/src/resources/schedule.ts b/src/resources/schedule.ts index ca595110..11b9fb47 100644 --- a/src/resources/schedule.ts +++ b/src/resources/schedule.ts @@ -13,9 +13,16 @@ // limitations under the License. import { EventMiddleware, Faas, ScheduleMiddleware } from '../faas'; -type Frequency = 'days' | 'hours' | 'minutes'; +const Frequencies = [ + 'day', + 'days', + 'hour', + 'hours', + 'minute', + 'minutes', +] as const; -const FREQUENCIES: Frequency[] = ['days', 'hours', 'minutes']; +export type Frequency = (typeof Frequencies)[number]; export class RateWorkerOptions { public readonly description: string; @@ -65,9 +72,11 @@ class Rate { ); } - if (!FREQUENCIES.includes(normalizedFrequency)) { + if (!Frequencies.includes(normalizedFrequency)) { throw new Error( - `invalid rate expression, frequency must be one of ${FREQUENCIES}, received ${frequency}` + `invalid rate expression, frequency must one of [${Frequencies.join( + ', ' + )}] received ${frequency}` ); } @@ -122,7 +131,7 @@ class Schedule { /** * Run this schedule on the provided frequency. * - * @param rate to run the schedule, e.g. '7 days'. All rates accept a number and a frequency. Valid frequencies are 'days', 'hours' or 'minutes'. + * @param rate to run the schedule, e.g. '7 days'. All rates accept a number and a frequency. Valid frequencies are 'day/days', 'hour/hours' or 'minute/minutes'. * @param middleware the handler/middleware to run on a schedule * @returns A promise that resolves when the schedule worker stops running. */ @@ -131,7 +140,7 @@ class Schedule { ...middleware: ScheduleMiddleware[] ): Promise => { // handle singular frequencies. e.g. schedule('something').every('day') - if (FREQUENCIES.indexOf(`${rate}s` as Frequency) !== -1) { + if (Frequencies.indexOf(`${rate}s` as Frequency) !== -1) { rate = `1 ${rate}s`; // 'day' becomes '1 days' }