Skip to content

Instantly share code, notes, and snippets.

@coderbyheart
Created August 25, 2023 13:12
Show Gist options
  • Save coderbyheart/163c20c60dee2b8322a5bf582bfaadf2 to your computer and use it in GitHub Desktop.
Save coderbyheart/163c20c60dee2b8322a5bf582bfaadf2 to your computer and use it in GitHub Desktop.
Aki Salmi SoCraTes 2023 - Test Any Unit
import { assertEquals } from "https://deno.land/std@0.200.0/assert/mod.ts";
import {
assertSpyCall,
spy,
} from "https://deno.land/std@0.200.0/testing/mock.ts";
Deno.test("coin changer", () => {
assertEquals(coinChanger(200), [200]);
});
Deno.test("coin changer with collaborator", () => {
assertEquals(
coinChanger(200, { availableCoins: () => [100, 100] }),
[100, 100]
);
});
Deno.test("account is called", () => {
const notify = () => {};
const notifyStub = spy({ notify }, "notify");
coinChanger(200, { availableCoins: () => [100, 100] }, notifyStub);
assertSpyCall(notifyStub, 0, {
args: [200],
});
});
export const coinChanger = (
cents: number,
{ availableCoins }: { availableCoins: () => number[] } = {
availableCoins: () => [200, 100, 50, 20, 10, 5],
},
notify?: (cents: number) => void
): number[] => {
notify?.(cents);
const result: number[] = [];
for (const coin of availableCoins()) {
while (cents >= coin) {
result.push(coin);
cents -= coin;
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment