Skip to content

Commit

Permalink
add rate test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
HomelessDinosaur committed Jun 8, 2023
1 parent cc01646 commit 2aba969
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
32 changes: 28 additions & 4 deletions src/resources/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
});
Expand All @@ -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);
});
});
});
});
4 changes: 2 additions & 2 deletions src/resources/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Frequencies = [
'minutes',
] as const;

type Frequency = (typeof Frequencies)[number];
export type Frequency = (typeof Frequencies)[number];

export class RateWorkerOptions {
public readonly description: string;
Expand Down Expand Up @@ -131,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.
*/
Expand Down

0 comments on commit 2aba969

Please sign in to comment.