Testing: Exercise Solution
Below you can find a possible implementation for the Test exercise seen in the previous lesson.
We need to keep in mind that there are different possible ways to implement unit tests. Even if your solution differs from the one below, it does not necessarily mean that it is not correct.
import { assert, assertExists, assertEquals, assertArrayIncludes, assertStringIncludes, assertThrows } from "https://deno.land/std@0.123.0/testing/asserts.ts"; interface Orders { orderId: number; orderCategory: string; } class User { constructor( public firstName: string, public lastName: string, public age: number, public companyName: string, public orders?: Orders[], ) { } /** * Returns the user's full name. */ getFullname(): string { if (!this.firstName || ! this.lastName) { throw new Error('Invalid username!'); } return `${this.firstName} ${this.lastName}` } /** * Returns `TRUE` if the user is 18 years old or older. `FALSE` otherwise. */ isAdult(): boolean { return this.age >= 18; } /** * Returns the user fidelity level according to the orders made. * 0 orders: standard level. * 2+ orders: silver level. * 5+ orders: gold level. */ evaluateUserLevel(): 'standard' | 'silver' | 'gold' { if (!this.orders) { return 'standard'; } else if (this.orders.length >= 2 && this.orders.length < 5) { return 'silver'; } else if (this.orders.length >= 5) { return 'gold'; } return 'standard'; } } Deno.test("Test User Has Adult Age", () => { const testUser = new User("Francesco", "Leardini", 30, "Cool Eng. Spa"); assertExists(testUser); assert(testUser.isAdult()); }); Deno.test("Test User is Minor", () => { const testUser = new User("Luca", "Rossi", 17, "Cool Eng. Spa"); assertExists(testUser); assert(!testUser.isAdult()); }); Deno.test("Test User Company Contains a Target String", () => { const testUser = new User("Francesco", "Leardini", 30, "Cool Eng. Spa"); assertExists(testUser); assertExists(testUser.companyName); assertStringIncludes(testUser.companyName, "Spa"); }); Deno.test("Throw an Exception Invoking 'getFullname()' when User Name is undefined", () => { const testUser = new User("Francesco", "Leardini", 30, "Cool Eng. Spa"); testUser.firstName = undefined as any; assertThrows(() => testUser.getFullname()); }); Deno.test("Test 'evaluateUserLevel()' - case no orders made", () => { const testUser = new User("Francesco", "Leardini", 30, "Cool Eng. Spa"); assertExists(testUser); assertEquals(testUser.evaluateUserLevel(), "standard"); }); Deno.test("Test a Given Order is Included in the user's order list", () => { const ordersMade: Orders[] = [ { orderId: 11, orderCategory: '2GB RAM' }, { orderId: 32, orderCategory: 'Graphic Card Deluxe' }]; const expectedMatchOrder: Orders[] = [{ orderId: 32, orderCategory: 'Graphic Card Deluxe' }]; const testUser = new User("Francesco", "Leardini", 30, "Cool Eng. Spa", ordersMade); assertExists(testUser); assertEquals(testUser.orders, ordersMade); assertArrayIncludes(testUser.orders as Orders[], expectedMatchOrder); });
Deno tests solution
Explanation
Test User is Minor
-
Lines 66–70: We want to test that the
isAdult()
method returnsfalse
for aUser
withage <= 18
.
Therefore, we’ll create a sample object using 17 as value for the age property. -
Line 69: We need to negate (using the
!
symbol) the result of the method calltestUser.isAdult()
, since it returns ...