Question: Need to create testing codes using react vitest config for the following class code: Syllable.ts ( Model ) export class Syllable { public content: string;
Need to create testing codes using react vitest config for the following class code:
Syllable.ts Model
export class Syllable
public content: string;
public row: number;
public column: number;
constructorcontent: string, row: number, column: number
this.content content;
this.row row;
this.column column;
Puzzle Class Model:
The puzzle class will manage the state of the puzzle board, swaps, and scoring.
import Syllable from Syllable;
export class Puzzle
public board: Syllable;
public swaps: number;
public score: number;
private originalConfig: Syllable;
constructorinitialConfig: Syllable
this.board initialConfig;
this.swaps ;
this.score ;
this.originalConfig JSON.parseJSONstringifyinitialConfig; Save the initial configuration
Swaps two syllables based on their positions
swapSyllablesrow: number, col: number, row: number, col: number: void
const temp this.boardrowcol;
this.boardrowcol this.boardrowcol;
this.boardrowcol temp;
this.swaps;
this.updateScore;
Resets the board to its initial configuration
reset: void
this.board JSON.parseJSONstringifythisoriginalConfig;
this.swaps ;
this.score ;
Updates score based on how many correct syllables are in the correct place
updateScore: void
let newScore ;
for let row ; row this.board.length; row
for let col ; col this.boardrowlength; col
if thisboardrowcolcontent this.originalConfigrowcolcontent
newScore;
this.score newScore;
PuzzleController.ts
import Puzzle from modelsPuzzle;
import Syllable from modelsSyllable;
export class PuzzleController
private puzzle: Puzzle;
constructorpuzzle: Puzzle
this.puzzle puzzle;
handleSwaprow: number, col: number, row: number, col: number: void
this.puzzle.swapSyllablesrow col row col;
handleReset: void
this.puzzle.reset;
getPuzzleState: Syllable
return this.puzzle.board;
getScore: number
return this.puzzle.score;
getSwaps: number
return this.puzzle.swaps;
Puzzle.tsx Component
import React, useState from 'react';
import PuzzleController from utilsPuzzleController;
import Syllable from modelsSyllable;
interface PuzzleProps
initialConfig: Syllable;
const Puzzle: React.FC initialConfig
const puzzleController setPuzzleController useStatenew PuzzleControllernew PuzzleinitialConfig;
const selected setSelected useState row: number, col: number nullnull;
const handleSyllableClick row: number, col: number
if selected
puzzleController.handleSwapselectedrow, selected.col, row, col;
setSelectednull; Reset selection after swap
else
setSelected row, col ; Select first syllable
;
const handleReset
puzzleController.handleReset;
setSelectednull;
;
const board puzzleController.getPuzzleState;
return
Swaps: puzzleControllergetSwapsScore: puzzleControllergetScoreSwaps: puzzleControllergetSwapsScore: puzzleControllergetScoreSwaps: puzzleControllergetSwapsScore: puzzleControllergetScore
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
