Aki Salmi SoCraTes 2023 - Test Any Unit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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