Skip to content

Step 2 - Implementing Functions #10

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 9 commits into
base: ms-training
Choose a base branch
from
174 changes: 133 additions & 41 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
//write tests here
import { User, UserAPI } from "./index"
import { CustomError, UserAPI } from "./index"

describe('Tests will go here!', () => {
let users: UserAPI

beforeEach(() => {
users = new UserAPI()
users = new UserAPI(
[
{
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('gets a user by id', () => {
Expand All @@ -17,15 +43,20 @@ describe('Tests will go here!', () => {
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('returns an error if there is no user matching id', () => {
expect(() => users.getUserById(10)).toThrow()
try {
users.getUserById(10)
} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("No user found.")
expect(error.status).toEqual(404)
}
})

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

expect(result).toStrictEqual([
Expand Down Expand Up @@ -55,13 +86,7 @@ describe('Tests will go here!', () => {
])
})

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

expect(result).toStrictEqual(null)
})

it.skip('creates a user', () => {
it('creates a user', () => {
const newUser = {
id: 4,
name: "Yoda",
Expand All @@ -78,18 +103,25 @@ describe('Tests will go here!', () => {
})
})

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

expect(result).toStrictEqual(null)
expect(() => users.createUser(newUser)).toThrow()
try {
users.createUser(newUser)
} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("User with id already exists.")
expect(error.status).toEqual(405)
}
})

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

expect(result).toStrictEqual({
Expand All @@ -98,37 +130,97 @@ describe('Tests will go here!', () => {
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', () => {
it('returns CustomError if deleted user id does not exist', () => {
expect(() => users.deleteUserById(10)).toThrow()

try {
users.deleteUserById(10)
} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("No user with that id found.")
expect(error.status).toEqual(404)
}
})

it('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",
})
})

expect(users.getUsers()).toStrictEqual([
{
id: 1,
name: "Michelle",
age: 29,
color: "red",
},
{
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 user with same id already exists', () => {
const result = users.updateUser(1, {
id: 2,
name: "Michelle",
it('returns CustomError if user with same id but different name already exists', () => {
const updatedUser = {
id: 1,
name: "John",
age: 29,
color: "red",
})
}

expect(() => users.updateUser(1, updatedUser)).toThrow()

try {
users.updateUser(1, updatedUser)
} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("Different user with same id already exists.")
expect(error.status).toEqual(405)
}
})

expect(result).toStrictEqual(null)
it('returns CustomError if user by id does not exist', () => {
const updatedUser = {
id: 10,
name: "Michelle",
age: 29,
color: "red",
}

expect(() => users.updateUser(10, updatedUser)).toThrow()

try {
users.updateUser(10, updatedUser)
} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("No user found by that id.")
expect(error.status).toEqual(404)
}
})
})

104 changes: 64 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,78 @@ export interface User {
readonly color: string
}

export interface ErrorMessage {
readonly message: string
}

export class CustomError extends Error {
status: number

constructor(status: number, message: string) {
super(message);
this.status = status
Object.setPrototypeOf(this, CustomError.prototype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very curious why you included this. I have no idea if it's right or wrong or anything; this isn't something I did in my implementation and I'm curious as to why you did this.

}
}

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",
},
]
constructor(users: ReadonlyArray<User>) {
this.users = users
}

public getUserById = (id: number): User => {
const foundUser = this.users.find(user => user.id === id)
if (!foundUser) {
throw new CustomError(404, "No user found.")
} else {
return foundUser
}
}

public getUserById = (id: number): User | null => {
console.log("Get user by id")
public getUsers = (): ReadonlyArray<User> => {
return this.users
}

public getUsers = (): ReadonlyArray<User> | null => {
console.log("Get users")
}
public createUser = (user: User): User => {
const userIdExists = this.users.find(existingUser => existingUser.id === user.id)

public createUser = (user: User): User | null => {
console.log("Create user", user)
}
if (userIdExists) {
throw new CustomError(405, "User with id already exists.")
} else {
this.users = [user, ...this.users]
return user
}
}

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

public updateUser = (id: number, user: User): User | null => {
console.log("Update user")
if (user) {
this.users = this.users.filter(user => user.id !== id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

return user
} else throw new CustomError(404, "No user with that id found.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

braces

}

public updateUser = (id: number, updatedUser: User): User => {
let targetUser = this.users.find(existingUser => existingUser.id === id)

if (targetUser) {
this.users = this.users.map(user => {
if (user.id === id) {
if (user.name === updatedUser.name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

return { id, ...updatedUser }
} else {
throw new CustomError(405, "Different user with same id already exists.")
}
} else {
return user
}
})
return { id, ...updatedUser }
} else {
throw new CustomError(404, "No user found by that id.")
}
}
}
}