Commit 074c5d4a authored by Yannick Li's avatar Yannick Li
Browse files

Prettier: prettify codebase

parent cbee298b
Pipeline #242040 canceled with stages
in 0 seconds
This diff is collapsed.
......@@ -43,7 +43,7 @@ describe("Testing UI", () => {
const wrapper = mount(
<Cell index={3} value={"6"} onChange={() => ({})} error={false} />
);
const input = wrapper.find('input');
const input = wrapper.find("input");
expect(input.instance().value).toBe("6");
});
});
......@@ -38,15 +38,15 @@ beforeEach(() => {
* This test evaluates that checkLine return true only if
* the array contains all numbers from 1 to 9 without repeat.
*/
describe("checkLine", () => {
describe("checkLine", () => {
test("checkLine", () => {
const wrapper = shallow(<Game />);
const game = wrapper.instance();
expect(game.checkLine(3)).toBe(true);
game.handleChange(35, "1")
game.handleChange(35, "1");
expect(game.checkLine(3)).toBe(false);
game.handleChange(35, "2")
game.handleChange(35, "2");
expect(game.checkLine(3)).toBe(true);
});
});
......@@ -61,9 +61,9 @@ describe("checkColumn", () => {
const game = wrapper.instance();
expect(game.checkColumn(8)).toBe(true);
game.handleChange(35, "1")
game.handleChange(35, "1");
expect(game.checkColumn(8)).toBe(false);
game.handleChange(35, "2")
game.handleChange(35, "2");
expect(game.checkColumn(8)).toBe(true);
});
});
......@@ -78,9 +78,9 @@ describe("checkBlock", () => {
const game = wrapper.instance();
expect(game.checkBlock(5)).toBe(true);
game.handleChange(35, "1")
game.handleChange(35, "1");
expect(game.checkBlock(5)).toBe(false);
game.handleChange(35, "2")
game.handleChange(35, "2");
expect(game.checkBlock(5)).toBe(true);
});
});
......@@ -105,9 +105,11 @@ describe("validateLine", () => {
"","","","","","","","",""]
const wrapper = shallow(<Game />);
const game = wrapper.instance();
expect(game.checkLines()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkLines()).toStrictEqual([]);
});
......@@ -130,7 +132,9 @@ describe("validateLine", () => {
const game = wrapper.instance();
expect(game.checkLines()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkLines()).toStrictEqual([]);
});
......@@ -152,7 +156,9 @@ describe("validateLine", () => {
const game = wrapper.instance();
expect(game.checkLines()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkLines()).toStrictEqual([]);
});
......@@ -174,7 +180,9 @@ describe("validateLine", () => {
const game = wrapper.instance();
expect(game.checkLines()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkLines()).toStrictEqual([0, 1, 2, 3, 4, 5, 6, 7, 8]);
});
......@@ -196,7 +204,9 @@ describe("validateLine", () => {
const game = wrapper.instance();
expect(game.checkLines()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkLines()).toStrictEqual([]);
});
});
......@@ -223,7 +233,9 @@ describe("validateColumn", () => {
const game = wrapper.instance();
expect(game.checkColumns()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkColumns()).toStrictEqual([]);
});
......@@ -246,7 +258,9 @@ describe("validateColumn", () => {
const game = wrapper.instance();
expect(game.checkColumns()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkColumns()).toStrictEqual([]);
});
......@@ -268,7 +282,9 @@ describe("validateColumn", () => {
const game = wrapper.instance();
expect(game.checkColumns()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkColumns()).toStrictEqual([]);
});
......@@ -290,8 +306,20 @@ describe("validateColumn", () => {
const game = wrapper.instance();
expect(game.checkColumns()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
expect(game.checkColumns()).toStrictEqual([1, 10, 19, 28, 37, 46, 55, 64, 73]);
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkColumns()).toStrictEqual([
1,
10,
19,
28,
37,
46,
55,
64,
73,
]);
});
/**
......@@ -312,7 +340,9 @@ describe("validateColumn", () => {
const game = wrapper.instance();
expect(game.checkColumns()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkColumns()).toStrictEqual([]);
});
});
......@@ -339,7 +369,9 @@ describe("validateBlock", () => {
const game = wrapper.instance();
expect(game.checkBlocks()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkBlocks()).toStrictEqual([]);
});
......@@ -362,7 +394,9 @@ describe("validateBlock", () => {
const game = wrapper.instance();
expect(game.checkBlocks()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkBlocks()).toStrictEqual([]);
});
......@@ -384,7 +418,9 @@ describe("validateBlock", () => {
const game = wrapper.instance();
expect(game.checkBlocks()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkBlocks()).toStrictEqual([]);
});
......@@ -406,7 +442,9 @@ describe("validateBlock", () => {
const game = wrapper.instance();
expect(game.checkBlocks()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkBlocks()).toStrictEqual([0, 1, 2, 9, 10, 11, 18, 19, 20]);
});
......@@ -428,7 +466,9 @@ describe("validateBlock", () => {
const game = wrapper.instance();
expect(game.checkBlocks()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkBlocks()).toStrictEqual([]);
});
});
......@@ -455,7 +495,9 @@ describe("validateSudoku", () => {
const game = wrapper.instance();
expect(game.checkCells()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkCells()).toStrictEqual([]);
});
......@@ -478,7 +520,9 @@ describe("validateSudoku", () => {
const game = wrapper.instance();
expect(game.checkCells()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkCells()).toStrictEqual([]);
});
......@@ -498,9 +542,11 @@ describe("validateSudoku", () => {
"","6","5","7","4","3","1","8","2"]
const wrapper = shallow(<Game />);
const game = wrapper.instance();
expect(game.checkCells()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkCells()).toStrictEqual([]);
});
......@@ -520,9 +566,11 @@ describe("validateSudoku", () => {
"9","6","5","7","4","3","1","8","2"]
const wrapper = shallow(<Game />);
const game = wrapper.instance();
expect(game.checkCells()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkCells()).toStrictEqual([]);
});
......@@ -544,7 +592,9 @@ describe("validateSudoku", () => {
const game = wrapper.instance();
expect(game.checkCells()).toStrictEqual([]);
game.updateState(initVal.map(val =>({ value: val, modifiable: false, error: false })))
game.updateState(
initVal.map((val) => ({ value: val, modifiable: false, error: false }))
);
expect(game.checkCells()).toStrictEqual([0, 1]);
});
});
......@@ -85,7 +85,7 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
* Called after the component is rendered.
* It set a timer to refresh cells values.
*/
componentDidMount(): void {
componentDidMount(): void {
this.initFrom(generateStaticGrid(this.state.gridNum));
this.timerID = setInterval(() => this.updateGrid(), 1000);
}
......@@ -135,12 +135,9 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
this.state.cells[index].modifiable &&
this.modifiedCells[index] !== null
) {
session.transaction(
client.utils.ConsistencyLevel.None,
() => {
this.state.mvmap.setString(index, this.modifiedCells[index]);
}
);
session.transaction(client.utils.ConsistencyLevel.None, () => {
this.state.mvmap.setString(index, this.modifiedCells[index]);
});
}
}
this.timerID = setInterval(() => this.updateGrid(), 1000);
......@@ -171,12 +168,9 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
if (cells[index].modifiable) {
cells[index].value = "";
if (this.state.isConnected) {
session.transaction(
client.utils.ConsistencyLevel.None,
() => {
this.state.mvmap.setString(index, cells[index].value);
}
);
session.transaction(client.utils.ConsistencyLevel.None, () => {
this.state.mvmap.setString(index, cells[index].value);
});
} else {
this.modifiedCells[index] = "";
}
......@@ -235,7 +229,7 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
this.setState({ gridNum: gridNum, mvmap: mvmap });
this.initFrom(generateStaticGrid(gridNum));
}
render(): JSX.Element {
return (
<div className="sudoku">
......@@ -259,8 +253,13 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
</button>
</div>
<br />
<Grid cells={this.state.cells} isFinished={this.state.isFinished}
onChange={(index: number, value: string) => this.handleChange(index, value)} />
<Grid
cells={this.state.cells}
isFinished={this.state.isFinished}
onChange={(index: number, value: string) =>
this.handleChange(index, value)
}
/>
</div>
);
}
......@@ -269,7 +268,7 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
* Check if a line respect Sudoku lines rules.
* @param line The line number to be checked.
*/
checkLine(line: number): boolean {
checkLine(line: number): boolean {
assert.ok(line >= 0 && line < 9);
const cpt = Array(9).fill(0);
for (let column = 0; column < 9; column++) {
......@@ -417,7 +416,7 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
* Return a predefined Sudoku grid as a string.
* @param gridNum Desired grid number
*/
function generateStaticGrid(gridNum: string) {
function generateStaticGrid(gridNum: string) {
return GRIDS[gridNum];
}
......
......@@ -49,7 +49,17 @@ describe("Cells initialization", () => {
"1","8","7","5","2","9","","","3",
"2","3","4","6","1","8","7","9","5",
"","6","5","7","4","3","1","8","2"]
const wrapper = mount(<Grid cells={initVal.map(val =>({ value: val, modifiable: false, error: false }))} isFinished={false} onChange={() => ({})} />);
const wrapper = mount(
<Grid
cells={initVal.map((val) => ({
value: val,
modifiable: false,
error: false,
}))}
isFinished={false}
onChange={() => ({})}
/>
);
expect(wrapper.find({ id: "0" }).instance().value).toBe("6");
expect(wrapper.find({ id: "1" }).instance().value).toBe("");
......@@ -73,7 +83,17 @@ describe("Testing cells", () => {
"2","3","4","6","1","8","7","9","5",
"","6","5","7","4","3","1","8","2"]
const wrapper = shallow(<Grid cells={initVal.map(val =>({ value: val, modifiable: false, error: false }))} isFinished={false} onChange={() => ({})} />);
const wrapper = shallow(
<Grid
cells={initVal.map((val) => ({
value: val,
modifiable: false,
error: false,
}))}
isFinished={false}
onChange={() => ({})}
/>
);
expect(wrapper.find("Cell")).toHaveLength(81);
});
});
......@@ -95,7 +115,17 @@ describe("Testing status", () => {
"2","3","4","6","1","8","7","9","5",
"9","6","5","7","4","3","1","8","2"]
const wrapper = mount(<Grid cells={initVal.map(val =>({ value: val, modifiable: false, error: false }))} isFinished={true} onChange={() => ({})} />);
const wrapper = mount(
<Grid
cells={initVal.map((val) => ({
value: val,
modifiable: false,
error: false,
}))}
isFinished={true}
onChange={() => ({})}
/>
);
const status = wrapper.find({ id: "status" });
expect(status.text()).toBe("Sudoku completed");
});
......@@ -104,12 +134,62 @@ describe("Testing status", () => {
/**
* This test evaluates that cellsIndexOfBlock return a list containing index of all cells of the same block.
*/
describe("cellsIndexOfBlock", () => {
describe("cellsIndexOfBlock", () => {
test("cellsIndexOfBlock", () => {
expect(cellsIndexOfBlock(0)).toStrictEqual([0, 1, 2, 9, 10, 11, 18, 19, 20]);
expect(cellsIndexOfBlock(1)).toStrictEqual([3, 4, 5, 12, 13, 14, 21, 22, 23]);
expect(cellsIndexOfBlock(2)).toStrictEqual([6, 7, 8, 15, 16, 17, 24, 25, 26]);
expect(cellsIndexOfBlock(4)).toStrictEqual([30, 31, 32, 39, 40, 41, 48, 49, 50]);
expect(cellsIndexOfBlock(8)).toStrictEqual([60, 61, 62, 69, 70, 71, 78, 79, 80]);
expect(cellsIndexOfBlock(0)).toStrictEqual([
0,
1,
2,
9,
10,
11,
18,
19,
20,
]);
expect(cellsIndexOfBlock(1)).toStrictEqual([
3,
4,
5,
12,
13,
14,
21,
22,
23,
]);
expect(cellsIndexOfBlock(2)).toStrictEqual([
6,
7,
8,
15,
16,
17,
24,
25,
26,
]);
expect(cellsIndexOfBlock(4)).toStrictEqual([
30,
31,
32,
39,
40,
41,
48,
49,
50,
]);
expect(cellsIndexOfBlock(8)).toStrictEqual([
60,
61,
62,
69,
70,
71,
78,
79,
80,
]);
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment