Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into fix/webapp/game
Browse files Browse the repository at this point in the history
# Conflicts:
#	webapp/src/components/game/Game.js
#	webapp/src/tests/Dashboard.test.js
  • Loading branch information
Toto-hitori committed Apr 19, 2024
2 parents be901da + 8dffc4c commit 3d4054e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 82 deletions.
13 changes: 10 additions & 3 deletions webapp/src/components/dashboard/CustomGameMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ const CustomGameMenu = ({ isOpen, onClose }) => {
useEffect(() => {
async function fetchCategories() {
try {
const categoriesData = await gameCategories();
const formattedCategories = categoriesData.map(category => category.charAt(0).toUpperCase() + category.slice(1).toLowerCase());
let lang = i18n.language;
if (lang.includes("en"))
lang = "en";
else if (lang.includes("es"))
lang = "es"
else
lang = "en";
const categoriesData = await gameCategories(lang);
const formattedCategories = categoriesData.map(category => category.name);
setCategories(formattedCategories);
} catch (error) {
console.error("Error fetching game categories:", error);
}
}
fetchCategories();
}, []);
}, [i18n.language]);

const manageCategory = (category) => {
if (selectedCategories.includes(category)) {
Expand Down
102 changes: 51 additions & 51 deletions webapp/src/components/game/Game.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import AuthManager from "components/auth/AuthManager";

const authManager = new AuthManager();

export async function isActive() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/is-active");
}

export async function getCurrentGame() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/play");
}

export async function gameCategories() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/question-categories");
}

export async function gameModes() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/gamemodes");
}

export async function newGame(lang, gamemode, customGameDto) {
let requestAnswer;
if (gamemode === "CUSTOM") {
requestAnswer = await authManager.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/games/play?lang=" + lang + "&gamemode=" + gamemode, customGameDto);
} else {
requestAnswer = await authManager.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/games/play?lang=" + lang + "&gamemode=" + gamemode);
}

return requestAnswer;
}

export async function startRound(gameId) {
return await authManager.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/startRound");
}

export async function getCurrentQuestion(gameId) {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/question");
}

export async function changeLanguage(gameId, language) {
await authManager.getAxiosInstance().put(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/language?language=" + language);
}

export async function answerQuestion(gameId, aId) {
return await authManager.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/answer", {answer_id:aId});
}

export async function getGameDetails(gameId) {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/details");
}
import AuthManager from "components/auth/AuthManager";

const authManager = new AuthManager();

export async function isActive() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/is-active");
}

export async function getCurrentGame() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/play");
}

export async function gameCategories() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/question-categories");
}

export async function gameModes() {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/gamemodes");
}

export async function newGame(lang, gamemode, customGameDto) {
let requestAnswer;
if (gamemode === "CUSTOM") {
requestAnswer = await authManager.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/games/play?lang=" + lang + "&gamemode=" + gamemode, customGameDto);
} else {
requestAnswer = await authManager.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/games/play?lang=" + lang + "&gamemode=" + gamemode);
}

return requestAnswer;
}

export async function startRound(gameId) {
return await authManager.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/startRound");
}

export async function getCurrentQuestion(gameId) {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/question");
}

export async function changeLanguage(gameId, language) {
await authManager.getAxiosInstance().put(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/language?language=" + language);
}

export async function answerQuestion(gameId, aId) {
return await authManager.getAxiosInstance().post(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/answer", {answer_id:aId});
}

export async function getGameDetails(gameId) {
return await authManager.getAxiosInstance().get(process.env.REACT_APP_API_ENDPOINT + "/games/" + gameId + "/details");
}

18 changes: 0 additions & 18 deletions webapp/src/tests/Dashboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,8 @@ describe('Dashboard component', () => {
})

it('renders dashboard elements correctly', async () => {
await act(async () => {
render(<ChakraProvider theme={theme}><MemoryRouter><Dashboard/></MemoryRouter></ChakraProvider>);
});

await waitFor(() => {
expect(screen.getByTestId('Welcome')).toBeInTheDocument();
expect(screen.getByTestId('Play')).toBeInTheDocument();
expect(screen.getByText(/Game modes/i)).toBeInTheDocument();
expect(screen.getByText(/User info/i)).toBeInTheDocument();
});
});

it('navigates to the game route on "Play" button click', async () => {
await act(async () => {
render(<ChakraProvider theme={theme}><MemoryRouter><Dashboard/></MemoryRouter></ChakraProvider>);
});

const playButton = screen.getByTestId('Play');
fireEvent.click(playButton);

expect(screen.getByText("common.play")).toBeInTheDocument();
});
});
11 changes: 1 addition & 10 deletions webapp/src/tests/Results.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ jest.mock('react-i18next', () => ({

describe('Results Component', () => {
test('renders results with correct answers', () => {
const { getByText, getByTestId } = render(
<BrowserRouter>
<Results />
</BrowserRouter>
);

expect(getByText('Results')).toBeInTheDocument();
expect(getByText('Correct answers: 3')).toBeInTheDocument();
expect(getByTestId('GoBack')).toBeInTheDocument();
expect(getByTestId('GoBack')).toHaveTextContent('common.finish');

});

it('navigates to dashboard on button click', async () => {
Expand Down

0 comments on commit 3d4054e

Please sign in to comment.