diff --git a/src/a/component.spec.ts b/src/a/component.spec.ts index 4e237f6..d6581ea 100644 --- a/src/a/component.spec.ts +++ b/src/a/component.spec.ts @@ -7,7 +7,15 @@ describe('Calculator', () => { component = new Calculator(); }); - it('should exist', () => { - expect(component).toBeTruthy(); + it('should correctly add two numbers together', () => { + expect(component.add(10, 20)).toEqual(30); + }); + + it('should correctly subtract two numbers together', () => { + expect(component.subtract(20, 15)).toEqual(5); + }); + + it('should correctly sum a list of numbers', () => { + expect(component.sum([5, 10, 60])).toEqual(75); }); }); diff --git a/src/b/component.spec.ts b/src/b/component.spec.ts index 317eb62..4c82996 100644 --- a/src/b/component.spec.ts +++ b/src/b/component.spec.ts @@ -7,13 +7,18 @@ describe('Profile', () => { component = new Profile(); }); - // remember to remove the 'should exist' tests - they're just placeholders - it('should exist', () => { - expect(component).toBeTruthy(); + it('should correctly identify when the user is an admin', () => { + component.user = { isAdmin: true }; + expect(component.isAdmin).toBe(true); }); - it('should correctly identify whether the user is an admin', () => { - component.user = { isAdmin: true }; - expect(component.isAdmin).toBeDefined(); + it('should correctly identify when the user is not an admin', () => { + component.user = { isAdmin: false }; + expect(component.isAdmin).toBe(false); + }); + + it('should not be an admin if the user is undefined', () => { + component.user = void 0; + expect(component.isAdmin).toBe(false); }); }); diff --git a/src/b/component.ts b/src/b/component.ts index ac5256d..adfcedb 100644 --- a/src/b/component.ts +++ b/src/b/component.ts @@ -1,15 +1,12 @@ export interface User { name: string; - isAdmin: boolean; // 👀 + isAdmin: boolean; } export class Profile { public user?: User; - // If you have written adequate unit tests for this file, - // you'll find a bug! Have a go at fixing it and make sure your - // tests pass. I think we can use the user isAdmin field :) public get isAdmin(): boolean { - return false; + return !!this.user?.isAdmin; } } diff --git a/src/c/component.spec.ts b/src/c/component.spec.ts index 42c0b0f..4db15be 100644 --- a/src/c/component.spec.ts +++ b/src/c/component.spec.ts @@ -7,7 +7,29 @@ describe('SimpleBank', () => { component = new SimpleBank(); }); - it('should create', () => { - expect(component).toBeTruthy(); + it('should throw if trying to deposit 0', () => { + expect(() => component.deposit(0)).toThrow(); + }); + + it('should throw if trying to deposit an amount less than 0', () => { + expect(() => component.deposit(-5)).toThrow(); + }); + + it('should increase the balance when depositing a postive number', () => { + component.deposit(10); + expect(component.balance).toEqual(10); + }); + + it('should throw if trying to withdraw 0', () => { + expect(() => component.deposit(0)).toThrow(); + }); + + it('should throw if trying to withdraw an amount less than 0', () => { + expect(() => component.withdraw(-5)).toThrow(); + }); + + it('should decrease the balance when withdrawing a positive number', () => { + component.withdraw(20); + expect(component.balance).toEqual(-20); }); }); diff --git a/src/d/component.spec.ts b/src/d/component.spec.ts index 427c308..bf5285f 100644 --- a/src/d/component.spec.ts +++ b/src/d/component.spec.ts @@ -1,24 +1,29 @@ import { Component, User } from './component'; -const mockData: User = { id: 'foo', name: 'bar', age: 40 }; - describe('Component', () => { let component: Component; beforeEach(() => { component = new Component(); - component.setUser(mockData); }); it('should return age as undefined if no user', () => { + component.setUser(void 0); + expect(component.age).toBeUndefined(); }); it('should correctly identify the user age', () => { - expect(component.age).toEqual(mockData.age); + const user = mockUser(50); + component.setUser(user); + + expect(component.age).toEqual(user.age); }); it('should increment the age of the user', () => { + component.setUser(mockUser(40)); + component.incrementAge(); + expect(component.age).toEqual(41); }); @@ -30,7 +35,10 @@ describe('Component', () => { }); it('should decrement the age of the user', () => { + component.setUser(mockUser(40)); + component.decrementAge(); + expect(component.age).toEqual(39); }); @@ -40,4 +48,8 @@ describe('Component', () => { component.decrementAge(); }).toThrow(); }); + + function mockUser(age: number): User { + return { age }; + } }); diff --git a/src/e/component.spec.ts b/src/e/component.spec.ts index 88a4043..ab30795 100644 --- a/src/e/component.spec.ts +++ b/src/e/component.spec.ts @@ -1,13 +1,21 @@ -import { Engine } from './component'; +import { Engine, Status } from './component'; describe('Engine', () => { let component: Engine; beforeEach(() => { component = new Engine(); + jest.spyOn(console, 'log').mockImplementation(() => null); }); - it('should exist', () => { - expect(component).toBeTruthy(); + it('should start the engine', () => { + component.start(); + expect(component.status).toBe(Status.started); + }); + + it('should stop the engine', () => { + component.start(); + component.stop(); + expect(component.status).toBe(Status.stopped); }); }); diff --git a/src/e/component.ts b/src/e/component.ts index ec3ed34..0726895 100644 --- a/src/e/component.ts +++ b/src/e/component.ts @@ -1,19 +1,22 @@ -export type Status = 'started' | 'stopped'; +export enum Status { + started, + stopped +} export class Engine { - private _status: Status = 'stopped'; + private _status: Status = Status.stopped; - public get status(): 'started' | 'stopped' { + public get status(): Status { return this._status; } public start(): void { - this._status = 'started'; + this._status = Status.started; console.log('starting'); } public stop(): void { - this._status = 'stopped'; + this._status = Status.stopped; console.log('stopping'); } } diff --git a/src/f/component.spec.ts b/src/f/component.spec.ts index 034856c..b0c19ec 100644 --- a/src/f/component.spec.ts +++ b/src/f/component.spec.ts @@ -1,4 +1,4 @@ -import { Shop } from './component'; +import { Car, CarColor, Shop } from './component'; describe('Shop', () => { let component: Shop; @@ -7,7 +7,15 @@ describe('Shop', () => { component = new Shop(); }); - it('should exist', () => { - expect(component).toBeTruthy(); + it('should add an order to the list of orders', () => { + const car = mockCar('white'); + + component.newOrder(car, 1000); + + expect(component.orders).toEqual([expect.objectContaining({ car })]); }); + + function mockCar(color: CarColor): Car { + return { color, make: 'Audi' }; + } }); diff --git a/src/g/component.spec.ts b/src/g/component.spec.ts index f6a6d89..b0bbad9 100644 --- a/src/g/component.spec.ts +++ b/src/g/component.spec.ts @@ -1,4 +1,4 @@ -import { CustomColumnHeader } from './component'; +import { ColumnApi, ColumnHeaderParams, CustomColumnHeader } from './component'; describe('CustomColumnHeader', () => { let component: CustomColumnHeader; @@ -7,7 +7,65 @@ describe('CustomColumnHeader', () => { component = new CustomColumnHeader(); }); - it('should exist', () => { - expect(component).toBeTruthy(); + it('should return undefined if no display name', () => { + expect(component.displayName).toBeUndefined(); }); + + it('should set the display name on init', () => { + const params = mockParams('foo', {}); + + component.init(params); + + expect(component.displayName).toEqual(params.headerName); + }); + + it('should not be expanded if params are undefined', () => { + expect(component.isExpanded).toBe(false); + }); + + it('should be expanded when column expanded is true', () => { + component.init(mockParams('foo', mockApi(true))); + + expect(component.isExpanded).toBe(true); + }); + + it('should not be expanded when column expanded is false', () => { + component.init(mockParams('foo', mockApi(false))); + + expect(component.isExpanded).toBe(false); + }); + + it('should not throw on toggle if params are undefined', () => { + expect(() => component.toggle()).not.toThrow(); + }); + + it('should set expanded to true on toggle if not expanded', () => { + const api = mockApi(false); + component.init(mockParams('foo', api)); + + component.toggle(); + + expect(api.setExpanded).toHaveBeenCalledWith(true); + }); + + it('should set expanded to false on toggle if expanded', () => { + const api = mockApi(true); + component.init(mockParams('foo', api)); + + component.toggle(); + + expect(api.setExpanded).toHaveBeenCalledWith(false); + }); + + function mockApi(isExpanded: boolean): ColumnApi { + return { + isExpandable: jest.fn(), + isExpanded: jest.fn(() => isExpanded), + setExpanded: jest.fn() + }; + } + + function mockParams(headerName: string, api: Partial): ColumnHeaderParams { + return { headerName, api }; + } }); diff --git a/src/g/component.ts b/src/g/component.ts index 7ac16bb..3a10e56 100644 --- a/src/g/component.ts +++ b/src/g/component.ts @@ -1,10 +1,12 @@ +export interface ColumnApi { + isExpandable: () => boolean; + isExpanded: () => boolean; + setExpanded: (expanded: boolean) => void; +} + export interface ColumnHeaderParams { headerName: string; - api: { - isExpandable: () => boolean; - isExpanded: () => boolean; - setExpanded: (expanded: boolean) => void; - }; + api: ColumnApi; } export interface ColumnHeader { diff --git a/src/h/component.spec.ts b/src/h/component.spec.ts index 6003845..c04369e 100644 --- a/src/h/component.spec.ts +++ b/src/h/component.spec.ts @@ -1,16 +1,35 @@ -import { AccountService, UserPortal } from './component'; +import { AccountService, User, UserPortal } from './component'; describe('UserPortal', () => { let component: UserPortal; let accountService: Partial; + let user: Partial; beforeEach(() => { - accountService = {}; + user = {}; + accountService = { + doLogin: jest.fn(), + doLogout: jest.fn(), + get user() { + return user; + } + }; component = new UserPortal(accountService); }); - it('should exist', () => { - expect(component).toBeTruthy(); + it('should get the user from the account service', () => { + user = { id: 'foo', name: 'bar' }; + expect(component.user).toEqual(user); + }); + + it('should get the account service to login', () => { + component.login(); + expect(accountService.doLogin).toHaveBeenCalled(); + }); + + it('should get the account service to logout', () => { + component.logout(); + expect(accountService.doLogout).toHaveBeenCalled(); }); }); @@ -21,7 +40,20 @@ describe('AccountService', () => { service = new AccountService(); }); - it('should exist', () => { - expect(service).toBeTruthy(); + it('should set the user on login', () => { + service.doLogin('bar'); + expect(service.user).toEqual(expect.objectContaining({ id: 'bar' })); + }); + + it('should reset the user on logout', () => { + service.doLogin('bar'); + + service.doLogout(); + + expect(service.user).toBeUndefined(); + }); + + it('should not throw on logout if user is undefined', () => { + expect(() => service.doLogout()).not.toThrow(); }); });