diff --git a/deps.ts b/deps.ts index 3a7e9f6..3f776ea 100644 --- a/deps.ts +++ b/deps.ts @@ -1,5 +1,6 @@ export { assert, + assertEquals, assertStrictEquals, assertThrows, } from "https://deno.land/std@0.87.0/testing/asserts.ts"; diff --git a/src/repositories/locales.test.ts b/src/repositories/locales.test.ts index c71657a..33c0be1 100644 --- a/src/repositories/locales.test.ts +++ b/src/repositories/locales.test.ts @@ -1,10 +1,13 @@ -import { assertStrictEquals } from "../../deps.ts"; -import { getLocaleById } from "./locales.ts"; +import { assertEquals } from "../../deps.ts"; +import { getLocaleById, Locale } from "./locales.ts"; Deno.test("Get locale by id", () => { const id = 25; const actual = getLocaleById(id); - const expected = "Salut le Monde!"; + const expected: Locale = { + locale: "French", + message: "Salut le Monde!", + }; - assertStrictEquals(actual, expected, `Expected ${expected}; got ${actual}`); + assertEquals(actual, expected, `Expected ${expected}; got ${actual}`); }); diff --git a/src/repositories/locales.ts b/src/repositories/locales.ts index bce13ed..b425786 100644 --- a/src/repositories/locales.ts +++ b/src/repositories/locales.ts @@ -1,9 +1,17 @@ import { locales } from "./locales.data.ts"; +export type Locale = { + locale: string; + message: string; +}; + export const localesSize = Object.keys(locales).length; /** * Return a given locale identified by its id * @param id unique identifier */ -export const getLocaleById = (id: number) => Object.values(locales)[id - 1]; +export const getLocaleById = (id: number): Locale => ({ + locale: Object.keys(locales)[id - 1], + message: Object.values(locales)[id - 1], +}); diff --git a/src/usecases/hello.test.ts b/src/usecases/hello.test.ts index 2dfc985..99d4962 100644 --- a/src/usecases/hello.test.ts +++ b/src/usecases/hello.test.ts @@ -1,6 +1,6 @@ -import { assertStrictEquals } from "../../deps.ts"; +import { assertEquals, assertStrictEquals } from "../../deps.ts"; import { ID } from "../types/id.ts"; -import { sayHello, sayRandomHello } from "./hello.ts"; +import { HelloResponse, sayHello, sayRandomHello } from "./hello.ts"; Deno.test("Random Hello", () => { const result = sayRandomHello(); @@ -10,17 +10,23 @@ Deno.test("Random Hello", () => { }); Deno.test("Specific Hello", () => { - type TestCase = { in: ID; want: string }; + type TestCase = { in: ID; want: HelloResponse }; const testCases: TestCase[] = [ - { in: new ID(25), want: "Salut le Monde!" }, - { in: new ID(21), want: "Hello World!" }, + { + in: new ID(25), + want: { id: 25, locale: "French", message: "Salut le Monde!" }, + }, + { + in: new ID(21), + want: { id: 21, locale: "English", message: "Hello World!" }, + }, ]; for (const test of testCases) { const actual = sayHello(test.in); const expected = test.want; - assertStrictEquals(actual, expected); + assertEquals(actual, expected); } }); diff --git a/src/usecases/hello.ts b/src/usecases/hello.ts index 266db5d..73a43ab 100644 --- a/src/usecases/hello.ts +++ b/src/usecases/hello.ts @@ -1,11 +1,7 @@ -import { getLocaleById, localesSize } from "../repositories/locales.ts"; +import { getLocaleById, Locale, localesSize } from "../repositories/locales.ts"; import { ID } from "../types/id.ts"; -type HelloResponse = { - id: number; - locale: string; - message: string; -}; +export type HelloResponse = Locale & { id: number }; /** * Display the iconic Hello, World in a random locale @@ -20,10 +16,8 @@ export const sayRandomHello = () => { * Display the iconic Hello, World in a locale identified by id * @param id Unique identifier */ -export const sayHello = (id: ID): HelloResponse => { - return { - id: id.value, - locale: "", - message: getLocaleById(id.value), - }; -}; +export const sayHello = (id: ID): HelloResponse => ({ + id: id.value, + locale: getLocaleById(id.value).locale, + message: getLocaleById(id.value).message, +});