Update tests to match new implementations

This commit is contained in:
Arnaud Vergnet 2020-08-06 12:09:17 +02:00
parent c86281cbd2
commit 1be913c5aa
4 changed files with 78 additions and 60 deletions

View file

@ -64,7 +64,7 @@ test('connect good credentials', () => {
.mockImplementationOnce(() => {
return Promise.resolve(true);
});
return expect(c.connect('email', 'password')).resolves.toBeTruthy();
return expect(c.connect('email', 'password')).resolves.toBe(undefined);
});
test('connect good credentials no consent', () => {
@ -100,7 +100,7 @@ test('connect good credentials, fail save token', () => {
return Promise.reject(false);
});
return expect(c.connect('email', 'password')).rejects.toBe(
ERROR_TYPE.UNKNOWN,
ERROR_TYPE.TOKEN_SAVE,
);
});
@ -125,7 +125,7 @@ test('connect bogus response 1', () => {
});
});
return expect(c.connect('email', 'password')).rejects.toBe(
ERROR_TYPE.CONNECTION_ERROR,
ERROR_TYPE.SERVER_ERROR,
);
});
@ -188,7 +188,7 @@ test('authenticatedRequest error bogus response', () => {
});
return expect(
c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'),
).rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
).rejects.toBe(ERROR_TYPE.SERVER_ERROR);
});
test('authenticatedRequest connection error', () => {
@ -213,5 +213,5 @@ test('authenticatedRequest error no token', () => {
});
return expect(
c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'),
).rejects.toBe(ERROR_TYPE.UNKNOWN);
).rejects.toBe(ERROR_TYPE.TOKEN_RETRIEVE);
});

View file

@ -5,36 +5,38 @@ import GridManager from '../logic/GridManager';
import ScoreManager from '../logic/ScoreManager';
import Piece from '../logic/Piece';
let colors = {
let theme = {
colors: {
tetrisBackground: '#000002',
},
};
jest.mock('../ScoreManager');
jest.mock('../logic/ScoreManager');
afterAll(() => {
jest.restoreAllMocks();
});
test('getEmptyLine', () => {
let g = new GridManager(2, 2, colors);
let g = new GridManager(2, 2, theme);
expect(g.getEmptyLine(2)).toStrictEqual([
{color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
]);
expect(g.getEmptyLine(-1)).toStrictEqual([]);
});
test('getEmptyGrid', () => {
let g = new GridManager(2, 2, colors);
let g = new GridManager(2, 2, theme);
expect(g.getEmptyGrid(2, 2)).toStrictEqual([
[
{color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
],
[
{color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
],
]);
@ -43,7 +45,7 @@ test('getEmptyGrid', () => {
});
test('getLinesToClear', () => {
let g = new GridManager(2, 2, colors);
let g = new GridManager(2, 2, theme);
g.getCurrentGrid()[0][0].isEmpty = false;
g.getCurrentGrid()[0][1].isEmpty = false;
let coord = [{x: 1, y: 0}];
@ -59,15 +61,15 @@ test('getLinesToClear', () => {
});
test('clearLines', () => {
let g = new GridManager(2, 2, colors);
let g = new GridManager(2, 2, theme);
let grid = [
[
{color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
],
[
{color: '0', isEmpty: true},
{color: '0', isEmpty: true},
{color: '0', isEmpty: true, key: '0'},
{color: '0', isEmpty: true, key: '1'},
],
];
g.getCurrentGrid()[1][0].color = '0';
@ -77,19 +79,19 @@ test('clearLines', () => {
g.clearLines([1], scoreManager);
grid = [
[
{color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
],
[
{color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
],
];
expect(g.getCurrentGrid()).toStrictEqual(grid);
});
test('freezeTetromino', () => {
let g = new GridManager(2, 2, colors);
let g = new GridManager(2, 2, theme);
let spy1 = jest
.spyOn(GridManager.prototype, 'getLinesToClear')
.mockImplementation(() => {});

View file

@ -4,9 +4,11 @@ import React from 'react';
import Piece from '../logic/Piece';
import ShapeI from '../Shapes/ShapeI';
let colors = {
let theme = {
colors: {
tetrisI: '#000001',
tetrisBackground: '#000002',
},
};
jest.mock('../Shapes/ShapeI');
@ -37,7 +39,7 @@ test('isPositionValid', () => {
];
let size = 2;
let p = new Piece(colors);
let p = new Piece(theme);
expect(p.isPositionValid(grid, size, size)).toBeTrue();
x = 1;
y = 0;
@ -65,7 +67,7 @@ test('isPositionValid', () => {
});
test('tryMove', () => {
let p = new Piece(colors);
let p = new Piece(theme);
const callbackMock = jest.fn();
let isValid = true;
let spy1 = jest
@ -98,7 +100,7 @@ test('tryMove', () => {
});
test('tryRotate', () => {
let p = new Piece(colors);
let p = new Piece(theme);
let isValid = true;
let spy1 = jest
.spyOn(Piece.prototype, 'isPositionValid')
@ -131,18 +133,30 @@ test('toGrid', () => {
return [{x: x, y: y}];
});
let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
return colors.tetrisI;
return theme.colors.tetrisI;
});
let grid = [
[{isEmpty: true}, {isEmpty: true}],
[{isEmpty: true}, {isEmpty: true}],
[
{isEmpty: true, key: '0'},
{isEmpty: true, key: '1'},
],
[
{isEmpty: true, key: '0'},
{isEmpty: true, key: '1'},
],
];
let expectedGrid = [
[{color: colors.tetrisI, isEmpty: false}, {isEmpty: true}],
[{isEmpty: true}, {isEmpty: true}],
[
{color: theme.colors.tetrisI, isEmpty: false, key: '0'},
{isEmpty: true, key: '1'},
],
[
{isEmpty: true, key: '0'},
{isEmpty: true, key: '1'},
],
];
let p = new Piece(colors);
let p = new Piece(theme);
p.toGrid(grid, true);
expect(grid).toStrictEqual(expectedGrid);
@ -153,16 +167,16 @@ test('toGrid', () => {
test('removeFromGrid', () => {
let gridOld = [
[
{color: colors.tetrisI, isEmpty: false},
{color: colors.tetrisI, isEmpty: false},
{color: colors.tetrisBackground, isEmpty: true},
{color: theme.colors.tetrisI, isEmpty: false, key: '0'},
{color: theme.colors.tetrisI, isEmpty: false, key: '1'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '2'},
],
];
let gridNew = [
[
{color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '0'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '1'},
{color: theme.colors.tetrisBackground, isEmpty: true, key: '2'},
],
];
let oldCoord = [
@ -175,9 +189,9 @@ test('removeFromGrid', () => {
return oldCoord;
});
let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
return colors.tetrisI;
return theme.colors.tetrisI;
});
let p = new Piece(colors);
let p = new Piece(theme);
p.removeFromGrid(gridOld);
expect(gridOld).toStrictEqual(gridNew);

View file

@ -4,7 +4,8 @@ import React from 'react';
import BaseShape from '../Shapes/BaseShape';
import ShapeI from '../Shapes/ShapeI';
const colors = {
const theme = {
colors: {
tetrisI: '#000001',
tetrisO: '#000002',
tetrisT: '#000003',
@ -12,20 +13,21 @@ const colors = {
tetrisZ: '#000005',
tetrisJ: '#000006',
tetrisL: '#000007',
},
};
test('constructor', () => {
expect(() => new BaseShape()).toThrow(Error);
let T = new ShapeI(colors);
let T = new ShapeI(theme);
expect(T.position.y).toBe(0);
expect(T.position.x).toBe(3);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]);
expect(T.getColor()).toBe(colors.tetrisI);
expect(T.getColor()).toBe(theme.colors.tetrisI);
});
test('move', () => {
let T = new ShapeI(colors);
let T = new ShapeI(theme);
T.move(0, 1);
expect(T.position.x).toBe(3);
expect(T.position.y).toBe(1);
@ -44,7 +46,7 @@ test('move', () => {
});
test('rotate', () => {
let T = new ShapeI(colors);
let T = new ShapeI(theme);
T.rotate(true);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]);
T.rotate(true);
@ -64,7 +66,7 @@ test('rotate', () => {
});
test('getCellsCoordinates', () => {
let T = new ShapeI(colors);
let T = new ShapeI(theme);
expect(T.getCellsCoordinates(false)).toStrictEqual([
{x: 0, y: 1},
{x: 1, y: 1},