Skip to content
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

Frontend event application unit test #355

Merged
merged 5 commits into from
Jan 3, 2022
Merged
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: 7 additions & 5 deletions frontend/src/Controllers/ApplicationsController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {getToken, getUserInfoLoggedIn} from "./AuthInfo";
import {getProfile} from "./ProfileController";

import * as ApplicationsController from "./ApplicationsController"
export async function getUserListInfo(list){
let details = []
list.forEach(id=>getProfile(id).then(res=>{
details.push({username:res.username,avatar:res.avatar,user_id:res.id})
}))
for (const id of list) {
await getProfile(id).then(res => {
details.push({username: res.username, avatar: res.avatar, user_id: res.id})
});
}
return details
}

Expand All @@ -20,7 +22,7 @@ export async function getApplicationsToAnEvent(post_id,playerApplications) {
"?type=" + playerApplications, options)
.then(response => response.json())
.then(async (result) => {
return await getUserListInfo(result.applicants)
return await ApplicationsController.getUserListInfo(result.applicants)
}
)
.then(d=>{console.log(d);return d})
Expand Down
102 changes: 102 additions & 0 deletions frontend/src/Tests/application.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import * as ApplicationsController from "../Controllers/ApplicationsController"


describe("Check if user information is listed correctly", () =>{
let originalFetch;
beforeEach(() => {
originalFetch = global.fetch;
global.fetch = jest.fn(() => Promise.resolve({
json: () => Promise.resolve({
"id": 1,
"first_name": "Berkay",
"last_name": "Doner",
"username": "berkaydoner",
"bio": "",
"fav_sport_1": "",
"fav_sport_2": "",
"fav_sport_3": "",
"location": "Istanbul",
"avatar": "",
"privacy": false,
"badges": [],
"birthday": "1999-11-13"
})}));})
afterEach(() => {
global.fetch = originalFetch;
});
it("Check if the response is in the correct format.",async () => {
const response = await ApplicationsController.getUserListInfo([1,2,3])
expect(response).toEqual([
{username:"berkaydoner",avatar:"",user_id:1},
{username:"berkaydoner",avatar:"",user_id:1},
{username:"berkaydoner",avatar:"",user_id:1}
])
})
})

describe("Check applicant list fetching", () =>{
let originalFetch;
beforeEach(() => {
originalFetch = global.fetch;
global.fetch = jest.fn(() => Promise.resolve({
json: () => Promise.resolve({
"applicants":[1,2,3]
})}));})
afterEach(() => {
global.fetch = originalFetch;
});
it("Check if the response is in the correct format.",async () => {
const spy1 = jest.spyOn(ApplicationsController,"getUserListInfo")
const response = await ApplicationsController.getApplicationsToAnEvent(0)
expect(response).toHaveLength(3)
expect(spy1).toHaveBeenCalledWith([1,2,3])
expect(spy1).toHaveBeenCalledTimes(1)
})
})
describe("Check application request", () =>{

let originalFetch;
let request;
beforeEach(() => {
localStorage.setItem('username', "berkaydoner")
localStorage.setItem('user_id', 1)
originalFetch = global.fetch;
global.fetch = jest.fn((x,options) => {request=options})
})
afterEach(() => {
global.fetch = originalFetch;
});
it("Check if the response is in the correct format.",async () => {
const response = await ApplicationsController.applyToEvent(0,"player")
expect(request.body).toEqual(
"{\"user\":\"1\",\"type\":\"player\"}"
)
expect(request.method).toEqual("POST")
})
localStorage.removeItem('username')
localStorage.removeItem('user_id')
})

describe("Check evaluation request", () =>{

let originalFetch;
let request;
beforeEach(() => {
localStorage.setItem('username', "berkaydoner")
localStorage.setItem('user_id', 1)
originalFetch = global.fetch;
global.fetch = jest.fn((x,options) => {request=options})
})
afterEach(() => {
global.fetch = originalFetch;
});
it("Check if the response is in the correct format.",async () => {
const response = await ApplicationsController.evaluateApplication(0,0,"player",true,1)
expect(request.body).toEqual(
"{\"user\":0,\"type\":\"player\",\"owner\":1,\"accept\":true}"
)
expect(request.method).toEqual("POST")
})
localStorage.removeItem('username')
localStorage.removeItem('user_id')
})