Skip to content

Commit

Permalink
Merge pull request #233 from Arquisoft/more_unit_testing
Browse files Browse the repository at this point in the history
More unit testing
  • Loading branch information
angelmaciasr authored Apr 27, 2024
2 parents 2db751b + f8cf8f2 commit 4ffa6a5
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 213 deletions.
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sonar.host.url=https://sonarcloud.io
sonar.language=js
sonar.projectName=wiq_en3a

sonar.coverage.exclusions=**/*.test.js
sonar.coverage.exclusions=**/*.test.js, **/*.test.ts, **/*.test.tsx
sonar.sources=webapp/src/components,users,wikidataservice,gatewayservice
sonar.sourceEncoding=UTF-8
sonar.exclusions=node_modules/**
Expand Down
31 changes: 0 additions & 31 deletions webapp/src/components/Game/Trivia/Dice.tsx

This file was deleted.

30 changes: 30 additions & 0 deletions webapp/src/components/Game/Trivia/categories.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getCategoryWithNumber, getCategoryColor, getCategoryColorWithNumber } from './categories';

describe('Test Category Functions', () => {
test('getCategoryWithNumber should return correct category', () => {
expect(getCategoryWithNumber(1)).toBe('Sports');
expect(getCategoryWithNumber(2)).toBe('Science');
});

test('getCategoryWithNumber should return undefined for non-existent number', () => {
expect(getCategoryWithNumber(6)).toBeUndefined();
});

test('getCategoryColor should return correct color', () => {
expect(getCategoryColor('Sports')).toBe('#1f71b3');
expect(getCategoryColor('Science')).toBe('#61a33a');
});

test('getCategoryColor should return undefined for non-existent category', () => {
expect(getCategoryColor('Math')).toBeUndefined();
});

test('getCategoryColorWithNumber should return correct color for a number', () => {
expect(getCategoryColorWithNumber(1)).toBe('#1f71b3');
expect(getCategoryColorWithNumber(2)).toBe('#61a33a');
});

test('getCategoryColorWithNumber should return undefined for non-existent number', () => {
expect(getCategoryColorWithNumber(6)).toBeUndefined();
});
});
42 changes: 42 additions & 0 deletions webapp/src/components/Game/Trivia/trivia_service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const axios = require('axios');
import {expect, jest, test} from '@jest/globals';

import {
getSportQuestions,
getScienceQuestions,
getHistoryQuestions,
getGeographyQuestions,
getEntertainmentQuestions
} from './trivia_service';

jest.mock('axios', () => {
return {
get: () => {return {data: [{
"text": "What is the capital of France?",
"answers": ["London", "Berlin", "Paris", "Madrid"],
"correctAnswer": 2
}] }}}})

describe('Test Question Functions', () => {

test('getSportQuestions should return a question', async () => {
const question = await getSportQuestions();
expect(question.text).toBe("What is the capital of France?");
});
test('getSportQuestions should return a question', async () => {
const question = await getScienceQuestions();
expect(question.text).toBe("What is the capital of France?");
});
test('getSportQuestions should return a question', async () => {
const question = await getHistoryQuestions();
expect(question.text).toBe("What is the capital of France?");
});
test('getSportQuestions should return a question', async () => {
const question = await getGeographyQuestions();
expect(question.text).toBe("What is the capital of France?");
});
test('getSportQuestions should return a question', async () => {
const question = await getEntertainmentQuestions();
expect(question.text).toBe("What is the capital of France?");
});
});
29 changes: 13 additions & 16 deletions webapp/src/components/Game/Trivia/trivia_service.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import { Question } from "@/src/services/question-service";
import axios from 'axios';

let url = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';



const getString = (string : string) : string => {
return "/Get" + string + "Questions";
}

export const getSportQuestions = async (): Promise<Question> => {
const response = await fetch(url + getString("Sport"));
const data = await response.json();
console.log(data);
return data[0];
const response = await axios.get(url + getString("Sport"));
return response.data[0];
};

export const getScienceQuestions = async (): Promise<Question> => {
const response = await fetch(url + getString("Chemistry"));
const data = await response.json();
return data[0];
const response = await axios.get(url + getString("Chemistry"));
return response.data[0];
};

export const getHistoryQuestions = async (): Promise<Question> => {
const response = await fetch(url + getString("History"));
const data = await response.json();
return data[0];
const response = await axios.get(url + getString("History"));
return response.data[0];
};

export const getGeographyQuestions = async (): Promise<Question> => {
const response = await fetch(url + getString("Geography"));
const data = await response.json();
return data[0];
const response = await axios.get(url + getString("Geography"));
return response.data[0];
};

export const getEntertainmentQuestions = async (): Promise<Question> => {
const response = await fetch(url + getString("Entertainment"));
const data = await response.json();
return data[0];
const response = await axios.get(url + getString("Entertainment"));
return response.data[0];
}


Expand Down
139 changes: 0 additions & 139 deletions webapp/src/components/ui/alert-dialog.tsx

This file was deleted.

26 changes: 0 additions & 26 deletions webapp/src/components/ui/popover.tsx

This file was deleted.

Empty file.

0 comments on commit 4ffa6a5

Please sign in to comment.