Skip to content

Step 1 - Create UserAPI class with stub functions, tests #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
133 changes: 130 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,134 @@
//write tests here
import { User, UserAPI } from "./index"

describe('Tests will go here!', () => {
it('should pass', () => {
expect(true).toBeTruthy()
let users: UserAPI

beforeEach(() => {
users = new UserAPI()
})

it('gets a user by id', () => {
const result = users.getUserById(2)

expect(result).toStrictEqual({
id: 2,
name: "Vintage Aaron",
age: 35,
color: "blue",
})
})

it.skip('returns null if there is no user matching id', () => {
const result = users.getUserById(10)

expect(result).toStrictEqual(null)
})

it.skip('gets all users', () => {
const result = users.getUsers()

expect(result).toStrictEqual([
{
id: 1,
name: "Michelle",
age: 30,
color: "rainbow",
},
{
id: 2,
name: "Vintage Aaron",
age: 35,
color: "blue",
},
{
id: 3,
name: "Derek",
age: 28,
color: "green",
},
{
name: "George",
age: 32,
color: "red",
},
])
})

it.skip('returns null if no users are found', () => {
const result = users.getUserById(10)

expect(result).toStrictEqual(null)
})

it.skip('creates a user', () => {
const newUser = {
id: 4,
name: "Yoda",
age: 10,
color: "Brown",
}
const result = users.createUser(newUser)

expect(result).toStrictEqual({
id: 4,
name: "Yoda",
age: 10,
color: "Brown",
})
})

it.skip('returns null if user id already exists', () => {
const result = users.createUser({
id: 1,
name: "Michelle",
color: "rainbow",
age: 30,
})

expect(result).toStrictEqual(null)
})

it.skip('deletes a user by id', () => {
const result = users.deleteUserById(1)

expect(result).toStrictEqual({
id: 1,
name: "Michelle",
age: 30,
color: "rainbow",
})
})

it.skip('returns null if deleted user id does not exist', () => {
const result = users.deleteUserById(5)

expect(result).toStrictEqual(false)
})

it.skip('updates a user by id', () => {
const result = users.updateUser(1, {
name: "Michelle",
age: 29,
color: "red",
})

expect(result).toStrictEqual({
id: 1,
name: "Michelle",
age: 29,
color: "red",
})
})

it.skip('returns null if user with same id already exists', () => {
const result = users.updateUser(1, {
id: 2,
name: "Michelle",
age: 29,
color: "red",
})

expect(result).toStrictEqual(null)
})
})
})
61 changes: 60 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
//Define class here
//Define class here

export interface User {
readonly id?: number
readonly name: string
readonly age: number
readonly color: string
}

export class UserAPI {
private users: ReadonlyArray<User>

constructor() {
this.users = [
{
id: 1,
name: "Michelle",
age: 30,
color: "rainbow",
},
{
id: 2,
name: "Vintage Aaron",
age: 35,
color: "blue",
},
{
id: 3,
name: "Derek",
age: 28,
color: "green",
},
{
name: "George",
age: 32,
color: "red",
},
]
}

public getUserById = (id: number): User | null => {
console.log("Get user by id")
}

public getUsers = (): ReadonlyArray<User> | null => {
console.log("Get users")
}

public createUser = (user: User): User | null => {
console.log("Create user", user)
}

public deleteUserById = (id: number): User | null => {
console.log("Create user by id")
}

public updateUser = (id: number, user: User): User | null => {
console.log("Update user")
}
}