Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/a/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
17 changes: 11 additions & 6 deletions src/b/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <User>{ isAdmin: true };
expect(component.isAdmin).toBe(true);
});

it('should correctly identify whether the user is an admin', () => {
component.user = <User>{ isAdmin: true };
expect(component.isAdmin).toBeDefined();
it('should correctly identify when the user is not an admin', () => {
component.user = <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);
});
});
7 changes: 2 additions & 5 deletions src/b/component.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
26 changes: 24 additions & 2 deletions src/c/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
20 changes: 16 additions & 4 deletions src/d/component.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});

Expand All @@ -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);
});

Expand All @@ -40,4 +48,8 @@ describe('Component', () => {
component.decrementAge();
}).toThrow();
});

function mockUser(age: number): User {
return <User>{ age };
}
});
14 changes: 11 additions & 3 deletions src/e/component.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
13 changes: 8 additions & 5 deletions src/e/component.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}
14 changes: 11 additions & 3 deletions src/f/component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Shop } from './component';
import { Car, CarColor, Shop } from './component';

describe('Shop', () => {
let component: Shop;
Expand All @@ -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 <Car>{ color, make: 'Audi' };
}
});
64 changes: 61 additions & 3 deletions src/g/component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomColumnHeader } from './component';
import { ColumnApi, ColumnHeaderParams, CustomColumnHeader } from './component';

describe('CustomColumnHeader', () => {
let component: CustomColumnHeader;
Expand All @@ -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 <ColumnApi>{
isExpandable: jest.fn(),
isExpanded: jest.fn(() => isExpanded),
setExpanded: jest.fn()
};
}

function mockParams(headerName: string, api: Partial<ColumnApi>): ColumnHeaderParams {
return <ColumnHeaderParams>{ headerName, api };
}
});
12 changes: 7 additions & 5 deletions src/g/component.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
44 changes: 38 additions & 6 deletions src/h/component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { AccountService, UserPortal } from './component';
import { AccountService, User, UserPortal } from './component';

describe('UserPortal', () => {
let component: UserPortal;
let accountService: Partial<AccountService>;
let user: Partial<User>;

beforeEach(() => {
accountService = {};
user = {};
accountService = {
doLogin: jest.fn(),
doLogout: jest.fn(),
get user() {
return <User>user;
}
};
component = new UserPortal(<AccountService>accountService);
});

it('should exist', () => {
expect(component).toBeTruthy();
it('should get the user from the account service', () => {
user = <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();
});
});

Expand All @@ -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();
});
});