Skip to content

Commit

Permalink
fix: warn about unknown function name (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjae Lee authored Dec 16, 2020
1 parent c41f49b commit dd46015
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/__tests__/_getFunctionalInterface.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import AlgoliaAnalytics from "../insights";
import { getFunctionalInterface } from "../_getFunctionalInterface";

describe("_getFunctionalInterface", () => {
let aa;

beforeEach(() => {
const analyticsInstance = new AlgoliaAnalytics({ requestFn: () => {} });
aa = getFunctionalInterface(analyticsInstance);
});

it("warn about unknown function name", () => {
console.warn = jest.fn();
aa("unknown-function");
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(
"The method `unknown-function` doesn't exist."
);
});
});
2 changes: 2 additions & 0 deletions lib/_getFunctionalInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export function getFunctionalInterface(instance: AlgoliaAnalytics) {
return (functionName: string, ...functionArguments: any[]) => {
if (functionName && isFunction((instance as any)[functionName])) {
instance[functionName](...functionArguments);
} else {
console.warn(`The method \`${functionName}\` doesn't exist.`);
}
};
}

0 comments on commit dd46015

Please sign in to comment.