Skip to content

Commit

Permalink
feat: Add all resolvers unit tests #12
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Apr 12, 2020
1 parent 90e4d20 commit d8cdd61
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/utils/memoize/memoize.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const cacheProp = Symbol.for(`[memoize]`);
const NO_ARGS = `__no_args__`;

export type GenericFunction<A extends any[] = any[], R = any> = (...args: A) => R;

const defaultKeyBuilder: GenericFunction<[], string> = (): string => {
return `__no_args__`;
return NO_ARGS;
};

const getCache = (target: any): Record<string, Map<any, any>> => {
Expand Down
22 changes: 18 additions & 4 deletions test/src/utils/memoize/memoize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ describe(`memoize`, () => {
return this.method1Mock(arg);
}

@memoize((...args: any[]) => args.join(`:`))
public method2(...args: any[]): string {
return this.method2Mock(...args);
@memoize((arg?: string) => arg || ``)
public method2(arg?: string): string {
return this.method2Mock(arg);
}
}

Expand Down Expand Up @@ -65,7 +65,7 @@ describe(`memoize`, () => {
expect(subjectB.method0Mock).toHaveBeenCalledTimes(1);
});

it(`should remember method values with one args`, () => {
it(`should remember method values with one arg`, () => {
expect(subjectA.method1(`foo`)).toBe(`method1 A:foo`);
expect(subjectA.method1Mock).toHaveBeenCalledTimes(1);
expect(subjectA.method1(`bar`)).toBe(`method1 A:bar`);
Expand All @@ -74,4 +74,18 @@ describe(`memoize`, () => {
expect(subjectA.method1(`bar`)).toBe(`method1 A:bar`);
expect(subjectA.method1Mock).toHaveBeenCalledTimes(2);
});

it(`should remember method values with optional one arg`, () => {
expect(subjectA.method2(`foo`)).toBe(`method2 A:foo`);
expect(subjectA.method2Mock).toHaveBeenCalledTimes(1);
expect(subjectA.method2(`bar`)).toBe(`method2 A:bar`);
expect(subjectA.method2Mock).toHaveBeenCalledTimes(2);
expect(subjectA.method2(`foo`)).toBe(`method2 A:foo`);
expect(subjectA.method2(`bar`)).toBe(`method2 A:bar`);
expect(subjectA.method2Mock).toHaveBeenCalledTimes(2);
expect(subjectA.method2()).toBe(`method2 A:`);
expect(subjectA.method2Mock).toHaveBeenCalledTimes(3);
expect(subjectA.method2()).toBe(`method2 A:`);
expect(subjectA.method2Mock).toHaveBeenCalledTimes(3);
});
});

0 comments on commit d8cdd61

Please sign in to comment.