Disable lint for test files

This commit is contained in:
Arnaud Vergnet 2020-08-05 21:09:04 +02:00
parent 1e81b2cd7b
commit 4cc9c61d72
9 changed files with 1221 additions and 1112 deletions

View file

@ -1,210 +1,217 @@
jest.mock('react-native-keychain'); /* eslint-disable */
import React from 'react'; import React from 'react';
import ConnectionManager from "../../src/managers/ConnectionManager"; import ConnectionManager from '../../src/managers/ConnectionManager';
import {ERROR_TYPE} from "../../src/utils/WebData"; import {ERROR_TYPE} from '../../src/utils/WebData';
let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native jest.mock('react-native-keychain');
const fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
const c = ConnectionManager.getInstance(); const c = ConnectionManager.getInstance();
afterEach(() => { afterEach(() => {
jest.restoreAllMocks(); jest.restoreAllMocks();
}); });
test('isLoggedIn yes', () => { test('isLoggedIn yes', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
return 'token'; .spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return 'token';
}); });
return expect(c.isLoggedIn()).toBe(true); return expect(c.isLoggedIn()).toBe(true);
}); });
test('isLoggedIn no', () => { test('isLoggedIn no', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
return null; .spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return null;
}); });
return expect(c.isLoggedIn()).toBe(false); return expect(c.isLoggedIn()).toBe(false);
}); });
test("isConnectionResponseValid", () => { test('connect bad credentials', () => {
let json = { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
error: 0, return Promise.resolve({
data: {token: 'token'} json: () => {
}; return {
expect(c.isConnectionResponseValid(json)).toBeTrue(); error: ERROR_TYPE.BAD_CREDENTIALS,
json = { data: {},
error: 2, };
data: {} },
}; });
expect(c.isConnectionResponseValid(json)).toBeTrue(); });
json = { return expect(c.connect('email', 'password')).rejects.toBe(
error: 0, ERROR_TYPE.BAD_CREDENTIALS,
data: {token: ''} );
};
expect(c.isConnectionResponseValid(json)).toBeFalse();
json = {
error: 'prout',
data: {token: ''}
};
expect(c.isConnectionResponseValid(json)).toBeFalse();
}); });
test("connect bad credentials", () => { test('connect good credentials', () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
json: () => { json: () => {
return { return {
error: ERROR_TYPE.BAD_CREDENTIALS, error: ERROR_TYPE.SUCCESS,
data: {} data: {token: 'token'},
}; };
}, },
})
}); });
return expect(c.connect('email', 'password')) });
.rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS); jest
.spyOn(ConnectionManager.prototype, 'saveLogin')
.mockImplementationOnce(() => {
return Promise.resolve(true);
});
return expect(c.connect('email', 'password')).resolves.toBeTruthy();
}); });
test("connect good credentials", () => { test('connect good credentials no consent', () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
json: () => { json: () => {
return { return {
error: ERROR_TYPE.SUCCESS, error: ERROR_TYPE.NO_CONSENT,
data: {token: 'token'} data: {},
}; };
}, },
})
}); });
jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => { });
return Promise.resolve(true); return expect(c.connect('email', 'password')).rejects.toBe(
}); ERROR_TYPE.NO_CONSENT,
return expect(c.connect('email', 'password')).resolves.toBeTruthy(); );
}); });
test("connect good credentials no consent", () => { test('connect good credentials, fail save token', () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
json: () => { json: () => {
return { return {
error: ERROR_TYPE.NO_CONSENT, error: ERROR_TYPE.SUCCESS,
data: {} data: {token: 'token'},
}; };
}, },
})
}); });
return expect(c.connect('email', 'password')) });
.rejects.toBe(ERROR_TYPE.NO_CONSENT); jest
.spyOn(ConnectionManager.prototype, 'saveLogin')
.mockImplementationOnce(() => {
return Promise.reject(false);
});
return expect(c.connect('email', 'password')).rejects.toBe(
ERROR_TYPE.UNKNOWN,
);
}); });
test("connect good credentials, fail save token", () => { test('connect connection error', () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.reject();
json: () => { });
return { return expect(c.connect('email', 'password')).rejects.toBe(
error: ERROR_TYPE.SUCCESS, ERROR_TYPE.CONNECTION_ERROR,
data: {token: 'token'} );
};
},
})
});
jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => {
return Promise.reject(false);
});
return expect(c.connect('email', 'password')).rejects.toBe(ERROR_TYPE.UNKNOWN);
}); });
test("connect connection error", () => { test('connect bogus response 1', () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.reject(); return Promise.resolve({
json: () => {
return {
thing: true,
wrong: '',
};
},
}); });
return expect(c.connect('email', 'password')) });
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); return expect(c.connect('email', 'password')).rejects.toBe(
ERROR_TYPE.CONNECTION_ERROR,
);
}); });
test("connect bogus response 1", () => { test('authenticatedRequest success', () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest
return Promise.resolve({ .spyOn(ConnectionManager.prototype, 'getToken')
json: () => { .mockImplementationOnce(() => {
return { return 'token';
thing: true,
wrong: '',
}
},
})
}); });
return expect(c.connect('email', 'password')) jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); return Promise.resolve({
json: () => {
return {
error: ERROR_TYPE.SUCCESS,
data: {coucou: 'toi'},
};
},
});
});
return expect(
c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'),
).resolves.toStrictEqual({coucou: 'toi'});
}); });
test('authenticatedRequest error wrong token', () => {
test("authenticatedRequest success", () => { jest
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { .spyOn(ConnectionManager.prototype, 'getToken')
return 'token'; .mockImplementationOnce(() => {
return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
json: () => { json: () => {
return { return {
error: ERROR_TYPE.SUCCESS, error: ERROR_TYPE.BAD_TOKEN,
data: {coucou: 'toi'} data: {},
}; };
}, },
})
}); });
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check')) });
.resolves.toStrictEqual({coucou: 'toi'}); return expect(
c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'),
).rejects.toBe(ERROR_TYPE.BAD_TOKEN);
}); });
test("authenticatedRequest error wrong token", () => { test('authenticatedRequest error bogus response', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
return 'token'; .spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
json: () => { json: () => {
return { return {
error: ERROR_TYPE.BAD_TOKEN, error: ERROR_TYPE.SUCCESS,
data: {} };
}; },
},
})
}); });
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check')) });
.rejects.toBe(ERROR_TYPE.BAD_TOKEN); return expect(
c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'),
).rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
}); });
test("authenticatedRequest error bogus response", () => { test('authenticatedRequest connection error', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
return 'token'; .spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.reject();
json: () => { });
return { return expect(
error: ERROR_TYPE.SUCCESS, c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'),
}; ).rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
},
})
});
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
}); });
test("authenticatedRequest connection error", () => { test('authenticatedRequest error no token', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
return 'token'; .spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return null;
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return expect(
return Promise.reject() c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'),
}); ).rejects.toBe(ERROR_TYPE.UNKNOWN);
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
});
test("authenticatedRequest error no token", () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return null;
});
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
.rejects.toBe(ERROR_TYPE.UNKNOWN);
}); });

View file

@ -1,319 +1,345 @@
/* eslint-disable */
import React from 'react'; import React from 'react';
import * as EquipmentBooking from "../../src/utils/EquipmentBooking"; import * as EquipmentBooking from '../../src/utils/EquipmentBooking';
import i18n from "i18n-js"; import i18n from 'i18n-js';
test('getISODate', () => { test('getISODate', () => {
let date = new Date("2020-03-05 12:00"); let date = new Date('2020-03-05 12:00');
expect(EquipmentBooking.getISODate(date)).toBe("2020-03-05"); expect(EquipmentBooking.getISODate(date)).toBe('2020-03-05');
date = new Date("2020-03-05"); date = new Date('2020-03-05');
expect(EquipmentBooking.getISODate(date)).toBe("2020-03-05"); expect(EquipmentBooking.getISODate(date)).toBe('2020-03-05');
date = new Date("2020-03-05 00:00"); // Treated as local time date = new Date('2020-03-05 00:00'); // Treated as local time
expect(EquipmentBooking.getISODate(date)).toBe("2020-03-04"); // Treated as UTC expect(EquipmentBooking.getISODate(date)).toBe('2020-03-04'); // Treated as UTC
}); });
test('getCurrentDay', () => { test('getCurrentDay', () => {
jest.spyOn(Date, 'now') jest
.mockImplementation(() => .spyOn(Date, 'now')
new Date('2020-01-14 14:50:35').getTime() .mockImplementation(() => new Date('2020-01-14 14:50:35').getTime());
); expect(EquipmentBooking.getCurrentDay().getTime()).toBe(
expect(EquipmentBooking.getCurrentDay().getTime()).toBe(new Date("2020-01-14").getTime()); new Date('2020-01-14').getTime(),
);
}); });
test('isEquipmentAvailable', () => { test('isEquipmentAvailable', () => {
jest.spyOn(Date, 'now') jest
.mockImplementation(() => .spyOn(Date, 'now')
new Date('2020-07-09').getTime() .mockImplementation(() => new Date('2020-07-09').getTime());
); let testDevice = {
let testDevice = { id: 1,
id: 1, name: 'Petit barbecue',
name: "Petit barbecue", caution: 100,
caution: 100, booked_at: [{begin: '2020-07-07', end: '2020-07-10'}],
booked_at: [{begin: "2020-07-07", end: "2020-07-10"}] };
}; expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse();
expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse();
testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-09"}]; testDevice.booked_at = [{begin: '2020-07-07', end: '2020-07-09'}];
expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse(); expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse();
testDevice.booked_at = [{begin: "2020-07-09", end: "2020-07-10"}]; testDevice.booked_at = [{begin: '2020-07-09', end: '2020-07-10'}];
expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse(); expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse();
testDevice.booked_at = [ testDevice.booked_at = [
{begin: "2020-07-07", end: "2020-07-8"}, {begin: '2020-07-07', end: '2020-07-8'},
{begin: "2020-07-10", end: "2020-07-12"}, {begin: '2020-07-10', end: '2020-07-12'},
]; ];
expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeTrue(); expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeTrue();
}); });
test('getFirstEquipmentAvailability', () => { test('getFirstEquipmentAvailability', () => {
jest.spyOn(Date, 'now') jest
.mockImplementation(() => .spyOn(Date, 'now')
new Date('2020-07-09').getTime() .mockImplementation(() => new Date('2020-07-09').getTime());
); let testDevice = {
let testDevice = { id: 1,
id: 1, name: 'Petit barbecue',
name: "Petit barbecue", caution: 100,
caution: 100, booked_at: [{begin: '2020-07-07', end: '2020-07-10'}],
booked_at: [{begin: "2020-07-07", end: "2020-07-10"}] };
}; expect(
expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-11").getTime()); EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime(),
testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-09"}]; ).toBe(new Date('2020-07-11').getTime());
expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-10").getTime()); testDevice.booked_at = [{begin: '2020-07-07', end: '2020-07-09'}];
testDevice.booked_at = [ expect(
{begin: "2020-07-07", end: "2020-07-09"}, EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime(),
{begin: "2020-07-10", end: "2020-07-16"}, ).toBe(new Date('2020-07-10').getTime());
]; testDevice.booked_at = [
expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-17").getTime()); {begin: '2020-07-07', end: '2020-07-09'},
testDevice.booked_at = [ {begin: '2020-07-10', end: '2020-07-16'},
{begin: "2020-07-07", end: "2020-07-09"}, ];
{begin: "2020-07-10", end: "2020-07-12"}, expect(
{begin: "2020-07-14", end: "2020-07-16"}, EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime(),
]; ).toBe(new Date('2020-07-17').getTime());
expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-13").getTime()); testDevice.booked_at = [
{begin: '2020-07-07', end: '2020-07-09'},
{begin: '2020-07-10', end: '2020-07-12'},
{begin: '2020-07-14', end: '2020-07-16'},
];
expect(
EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime(),
).toBe(new Date('2020-07-13').getTime());
}); });
test('getRelativeDateString', () => { test('getRelativeDateString', () => {
jest.spyOn(Date, 'now') jest
.mockImplementation(() => .spyOn(Date, 'now')
new Date('2020-07-09').getTime() .mockImplementation(() => new Date('2020-07-09').getTime());
); jest.spyOn(i18n, 't').mockImplementation((translationString: string) => {
jest.spyOn(i18n, 't') const prefix = 'screens.equipment.';
.mockImplementation((translationString: string) => { if (translationString === prefix + 'otherYear') return '0';
const prefix = "screens.equipment."; else if (translationString === prefix + 'otherMonth') return '1';
if (translationString === prefix + "otherYear") else if (translationString === prefix + 'thisMonth') return '2';
return "0"; else if (translationString === prefix + 'tomorrow') return '3';
else if (translationString === prefix + "otherMonth") else if (translationString === prefix + 'today') return '4';
return "1"; else return null;
else if (translationString === prefix + "thisMonth") });
return "2"; expect(EquipmentBooking.getRelativeDateString(new Date('2020-07-09'))).toBe(
else if (translationString === prefix + "tomorrow") '4',
return "3"; );
else if (translationString === prefix + "today") expect(EquipmentBooking.getRelativeDateString(new Date('2020-07-10'))).toBe(
return "4"; '3',
else );
return null; expect(EquipmentBooking.getRelativeDateString(new Date('2020-07-11'))).toBe(
} '2',
); );
expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-09"))).toBe("4"); expect(EquipmentBooking.getRelativeDateString(new Date('2020-07-30'))).toBe(
expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-10"))).toBe("3"); '2',
expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-11"))).toBe("2"); );
expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-30"))).toBe("2"); expect(EquipmentBooking.getRelativeDateString(new Date('2020-08-30'))).toBe(
expect(EquipmentBooking.getRelativeDateString(new Date("2020-08-30"))).toBe("1"); '1',
expect(EquipmentBooking.getRelativeDateString(new Date("2020-11-10"))).toBe("1"); );
expect(EquipmentBooking.getRelativeDateString(new Date("2021-11-10"))).toBe("0"); expect(EquipmentBooking.getRelativeDateString(new Date('2020-11-10'))).toBe(
'1',
);
expect(EquipmentBooking.getRelativeDateString(new Date('2021-11-10'))).toBe(
'0',
);
}); });
test('getValidRange', () => { test('getValidRange', () => {
let testDevice = { let testDevice = {
id: 1, id: 1,
name: "Petit barbecue", name: 'Petit barbecue',
caution: 100, caution: 100,
booked_at: [{begin: "2020-07-07", end: "2020-07-10"}] booked_at: [{begin: '2020-07-07', end: '2020-07-10'}],
}; };
let start = new Date("2020-07-11"); let start = new Date('2020-07-11');
let end = new Date("2020-07-15"); let end = new Date('2020-07-15');
let result = [ let result = [
"2020-07-11", '2020-07-11',
"2020-07-12", '2020-07-12',
"2020-07-13", '2020-07-13',
"2020-07-14", '2020-07-14',
"2020-07-15", '2020-07-15',
]; ];
expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result); expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(
testDevice.booked_at = [ result,
{begin: "2020-07-07", end: "2020-07-10"}, );
{begin: "2020-07-13", end: "2020-07-15"}, testDevice.booked_at = [
]; {begin: '2020-07-07', end: '2020-07-10'},
result = [ {begin: '2020-07-13', end: '2020-07-15'},
"2020-07-11", ];
"2020-07-12", result = ['2020-07-11', '2020-07-12'];
]; expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(
expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result); result,
);
testDevice.booked_at = [{begin: "2020-07-12", end: "2020-07-13"}]; testDevice.booked_at = [{begin: '2020-07-12', end: '2020-07-13'}];
result = ["2020-07-11"]; result = ['2020-07-11'];
expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result); expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(
testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-12"},]; result,
result = [ );
"2020-07-13", testDevice.booked_at = [{begin: '2020-07-07', end: '2020-07-12'}];
"2020-07-14", result = ['2020-07-13', '2020-07-14', '2020-07-15'];
"2020-07-15", expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(
]; result,
expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result); );
start = new Date("2020-07-14"); start = new Date('2020-07-14');
end = new Date("2020-07-14"); end = new Date('2020-07-14');
result = [ result = ['2020-07-14'];
"2020-07-14", expect(
]; EquipmentBooking.getValidRange(start, start, testDevice),
expect(EquipmentBooking.getValidRange(start, start, testDevice)).toStrictEqual(result); ).toStrictEqual(result);
expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result); expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(
expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual(result); result,
);
expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual(
result,
);
start = new Date("2020-07-14"); start = new Date('2020-07-14');
end = new Date("2020-07-17"); end = new Date('2020-07-17');
result = [ result = ['2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17'];
"2020-07-14", expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual(
"2020-07-15", result,
"2020-07-16", );
"2020-07-17",
];
expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual(result);
testDevice.booked_at = [{begin: "2020-07-17", end: "2020-07-17"}]; testDevice.booked_at = [{begin: '2020-07-17', end: '2020-07-17'}];
result = [ result = ['2020-07-14', '2020-07-15', '2020-07-16'];
"2020-07-14", expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(
"2020-07-15", result,
"2020-07-16", );
];
expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result);
testDevice.booked_at = [ testDevice.booked_at = [
{begin: "2020-07-12", end: "2020-07-13"}, {begin: '2020-07-12', end: '2020-07-13'},
{begin: "2020-07-15", end: "2020-07-20"}, {begin: '2020-07-15', end: '2020-07-20'},
]; ];
start = new Date("2020-07-11"); start = new Date('2020-07-11');
end = new Date("2020-07-23"); end = new Date('2020-07-23');
result = [ result = ['2020-07-21', '2020-07-22', '2020-07-23'];
"2020-07-21", expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(
"2020-07-22", result,
"2020-07-23", );
];
expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result);
}); });
test('generateMarkedDates', () => { test('generateMarkedDates', () => {
let theme = { let theme = {
colors: { colors: {
primary: "primary", primary: 'primary',
danger: "primary", danger: 'primary',
textDisabled: "primary", textDisabled: 'primary',
} },
} };
let testDevice = { let testDevice = {
id: 1, id: 1,
name: "Petit barbecue", name: 'Petit barbecue',
caution: 100, caution: 100,
booked_at: [{begin: "2020-07-07", end: "2020-07-10"}] booked_at: [{begin: '2020-07-07', end: '2020-07-10'}],
}; };
let start = new Date("2020-07-11"); let start = new Date('2020-07-11');
let end = new Date("2020-07-13"); let end = new Date('2020-07-13');
let range = EquipmentBooking.getValidRange(start, end, testDevice); let range = EquipmentBooking.getValidRange(start, end, testDevice);
let result = { let result = {
"2020-07-11": { '2020-07-11': {
startingDay: true, startingDay: true,
endingDay: false, endingDay: false,
color: theme.colors.primary color: theme.colors.primary,
}, },
"2020-07-12": { '2020-07-12': {
startingDay: false, startingDay: false,
endingDay: false, endingDay: false,
color: theme.colors.danger color: theme.colors.danger,
}, },
"2020-07-13": { '2020-07-13': {
startingDay: false, startingDay: false,
endingDay: true, endingDay: true,
color: theme.colors.primary color: theme.colors.primary,
}, },
}; };
expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); expect(
result = { EquipmentBooking.generateMarkedDates(true, theme, range),
"2020-07-11": { ).toStrictEqual(result);
startingDay: true, result = {
endingDay: false, '2020-07-11': {
color: theme.colors.textDisabled startingDay: true,
}, endingDay: false,
"2020-07-12": { color: theme.colors.textDisabled,
startingDay: false, },
endingDay: false, '2020-07-12': {
color: theme.colors.textDisabled startingDay: false,
}, endingDay: false,
"2020-07-13": { color: theme.colors.textDisabled,
startingDay: false, },
endingDay: true, '2020-07-13': {
color: theme.colors.textDisabled startingDay: false,
}, endingDay: true,
}; color: theme.colors.textDisabled,
expect(EquipmentBooking.generateMarkedDates(false, theme, range)).toStrictEqual(result); },
result = { };
"2020-07-11": { expect(
startingDay: true, EquipmentBooking.generateMarkedDates(false, theme, range),
endingDay: false, ).toStrictEqual(result);
color: theme.colors.textDisabled result = {
}, '2020-07-11': {
"2020-07-12": { startingDay: true,
startingDay: false, endingDay: false,
endingDay: false, color: theme.colors.textDisabled,
color: theme.colors.textDisabled },
}, '2020-07-12': {
"2020-07-13": { startingDay: false,
startingDay: false, endingDay: false,
endingDay: true, color: theme.colors.textDisabled,
color: theme.colors.textDisabled },
}, '2020-07-13': {
}; startingDay: false,
range = EquipmentBooking.getValidRange(end, start, testDevice); endingDay: true,
expect(EquipmentBooking.generateMarkedDates(false, theme, range)).toStrictEqual(result); color: theme.colors.textDisabled,
},
};
range = EquipmentBooking.getValidRange(end, start, testDevice);
expect(
EquipmentBooking.generateMarkedDates(false, theme, range),
).toStrictEqual(result);
testDevice.booked_at = [{begin: "2020-07-13", end: "2020-07-15"},]; testDevice.booked_at = [{begin: '2020-07-13', end: '2020-07-15'}];
result = { result = {
"2020-07-11": { '2020-07-11': {
startingDay: true, startingDay: true,
endingDay: false, endingDay: false,
color: theme.colors.primary color: theme.colors.primary,
}, },
"2020-07-12": { '2020-07-12': {
startingDay: false, startingDay: false,
endingDay: true, endingDay: true,
color: theme.colors.primary color: theme.colors.primary,
}, },
}; };
range = EquipmentBooking.getValidRange(start, end, testDevice); range = EquipmentBooking.getValidRange(start, end, testDevice);
expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); expect(
EquipmentBooking.generateMarkedDates(true, theme, range),
).toStrictEqual(result);
testDevice.booked_at = [{begin: "2020-07-12", end: "2020-07-13"},]; testDevice.booked_at = [{begin: '2020-07-12', end: '2020-07-13'}];
result = { result = {
"2020-07-11": { '2020-07-11': {
startingDay: true, startingDay: true,
endingDay: true, endingDay: true,
color: theme.colors.primary color: theme.colors.primary,
}, },
}; };
range = EquipmentBooking.getValidRange(start, end, testDevice); range = EquipmentBooking.getValidRange(start, end, testDevice);
expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); expect(
EquipmentBooking.generateMarkedDates(true, theme, range),
).toStrictEqual(result);
testDevice.booked_at = [ testDevice.booked_at = [
{begin: "2020-07-12", end: "2020-07-13"}, {begin: '2020-07-12', end: '2020-07-13'},
{begin: "2020-07-15", end: "2020-07-20"}, {begin: '2020-07-15', end: '2020-07-20'},
]; ];
start = new Date("2020-07-11"); start = new Date('2020-07-11');
end = new Date("2020-07-23"); end = new Date('2020-07-23');
result = { result = {
"2020-07-11": { '2020-07-11': {
startingDay: true, startingDay: true,
endingDay: true, endingDay: true,
color: theme.colors.primary color: theme.colors.primary,
}, },
}; };
range = EquipmentBooking.getValidRange(start, end, testDevice); range = EquipmentBooking.getValidRange(start, end, testDevice);
expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); expect(
EquipmentBooking.generateMarkedDates(true, theme, range),
).toStrictEqual(result);
result = { result = {
"2020-07-21": { '2020-07-21': {
startingDay: true, startingDay: true,
endingDay: false, endingDay: false,
color: theme.colors.primary color: theme.colors.primary,
}, },
"2020-07-22": { '2020-07-22': {
startingDay: false, startingDay: false,
endingDay: false, endingDay: false,
color: theme.colors.danger color: theme.colors.danger,
}, },
"2020-07-23": { '2020-07-23': {
startingDay: false, startingDay: false,
endingDay: true, endingDay: true,
color: theme.colors.primary color: theme.colors.primary,
}, },
}; };
range = EquipmentBooking.getValidRange(end, start, testDevice); range = EquipmentBooking.getValidRange(end, start, testDevice);
expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); expect(
EquipmentBooking.generateMarkedDates(true, theme, range),
).toStrictEqual(result);
}); });

View file

@ -1,210 +1,222 @@
/* eslint-disable */
import React from 'react'; import React from 'react';
import * as Planning from "../../src/utils/Planning"; import * as Planning from '../../src/utils/Planning';
test('isDescriptionEmpty', () => { test('isDescriptionEmpty', () => {
expect(Planning.isDescriptionEmpty("")).toBeTrue(); expect(Planning.isDescriptionEmpty('')).toBeTrue();
expect(Planning.isDescriptionEmpty(" ")).toBeTrue(); expect(Planning.isDescriptionEmpty(' ')).toBeTrue();
// noinspection CheckTagEmptyBody // noinspection CheckTagEmptyBody
expect(Planning.isDescriptionEmpty("<p></p>")).toBeTrue(); expect(Planning.isDescriptionEmpty('<p></p>')).toBeTrue();
expect(Planning.isDescriptionEmpty("<p> </p>")).toBeTrue(); expect(Planning.isDescriptionEmpty('<p> </p>')).toBeTrue();
expect(Planning.isDescriptionEmpty("<p><br></p>")).toBeTrue(); expect(Planning.isDescriptionEmpty('<p><br></p>')).toBeTrue();
expect(Planning.isDescriptionEmpty("<p><br></p><p><br></p>")).toBeTrue(); expect(Planning.isDescriptionEmpty('<p><br></p><p><br></p>')).toBeTrue();
expect(Planning.isDescriptionEmpty("<p><br><br><br></p>")).toBeTrue(); expect(Planning.isDescriptionEmpty('<p><br><br><br></p>')).toBeTrue();
expect(Planning.isDescriptionEmpty("<p><br>")).toBeTrue(); expect(Planning.isDescriptionEmpty('<p><br>')).toBeTrue();
expect(Planning.isDescriptionEmpty(null)).toBeTrue(); expect(Planning.isDescriptionEmpty(null)).toBeTrue();
expect(Planning.isDescriptionEmpty(undefined)).toBeTrue(); expect(Planning.isDescriptionEmpty(undefined)).toBeTrue();
expect(Planning.isDescriptionEmpty("coucou")).toBeFalse(); expect(Planning.isDescriptionEmpty('coucou')).toBeFalse();
expect(Planning.isDescriptionEmpty("<p>coucou</p>")).toBeFalse(); expect(Planning.isDescriptionEmpty('<p>coucou</p>')).toBeFalse();
}); });
test('isEventDateStringFormatValid', () => { test('isEventDateStringFormatValid', () => {
expect(Planning.isEventDateStringFormatValid("2020-03-21 09:00")).toBeTrue(); expect(Planning.isEventDateStringFormatValid('2020-03-21 09:00')).toBeTrue();
expect(Planning.isEventDateStringFormatValid("3214-64-12 01:16")).toBeTrue(); expect(Planning.isEventDateStringFormatValid('3214-64-12 01:16')).toBeTrue();
expect(Planning.isEventDateStringFormatValid("3214-64-12 01:16:00")).toBeFalse(); expect(
expect(Planning.isEventDateStringFormatValid("3214-64-12 1:16")).toBeFalse(); Planning.isEventDateStringFormatValid('3214-64-12 01:16:00'),
expect(Planning.isEventDateStringFormatValid("3214-f4-12 01:16")).toBeFalse(); ).toBeFalse();
expect(Planning.isEventDateStringFormatValid("sqdd 09:00")).toBeFalse(); expect(Planning.isEventDateStringFormatValid('3214-64-12 1:16')).toBeFalse();
expect(Planning.isEventDateStringFormatValid("2020-03-21")).toBeFalse(); expect(Planning.isEventDateStringFormatValid('3214-f4-12 01:16')).toBeFalse();
expect(Planning.isEventDateStringFormatValid("2020-03-21 truc")).toBeFalse(); expect(Planning.isEventDateStringFormatValid('sqdd 09:00')).toBeFalse();
expect(Planning.isEventDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse(); expect(Planning.isEventDateStringFormatValid('2020-03-21')).toBeFalse();
expect(Planning.isEventDateStringFormatValid("garbage")).toBeFalse(); expect(Planning.isEventDateStringFormatValid('2020-03-21 truc')).toBeFalse();
expect(Planning.isEventDateStringFormatValid("")).toBeFalse(); expect(
expect(Planning.isEventDateStringFormatValid(undefined)).toBeFalse(); Planning.isEventDateStringFormatValid('3214-64-12 1:16:65'),
expect(Planning.isEventDateStringFormatValid(null)).toBeFalse(); ).toBeFalse();
expect(Planning.isEventDateStringFormatValid('garbage')).toBeFalse();
expect(Planning.isEventDateStringFormatValid('')).toBeFalse();
expect(Planning.isEventDateStringFormatValid(undefined)).toBeFalse();
expect(Planning.isEventDateStringFormatValid(null)).toBeFalse();
}); });
test('stringToDate', () => { test('stringToDate', () => {
let testDate = new Date(); let testDate = new Date();
expect(Planning.stringToDate(undefined)).toBeNull(); expect(Planning.stringToDate(undefined)).toBeNull();
expect(Planning.stringToDate("")).toBeNull(); expect(Planning.stringToDate('')).toBeNull();
expect(Planning.stringToDate("garbage")).toBeNull(); expect(Planning.stringToDate('garbage')).toBeNull();
expect(Planning.stringToDate("2020-03-21")).toBeNull(); expect(Planning.stringToDate('2020-03-21')).toBeNull();
expect(Planning.stringToDate("09:00:00")).toBeNull(); expect(Planning.stringToDate('09:00:00')).toBeNull();
expect(Planning.stringToDate("2020-03-21 09:g0")).toBeNull(); expect(Planning.stringToDate('2020-03-21 09:g0')).toBeNull();
expect(Planning.stringToDate("2020-03-21 09:g0:")).toBeNull(); expect(Planning.stringToDate('2020-03-21 09:g0:')).toBeNull();
testDate.setFullYear(2020, 2, 21); testDate.setFullYear(2020, 2, 21);
testDate.setHours(9, 0, 0, 0); testDate.setHours(9, 0, 0, 0);
expect(Planning.stringToDate("2020-03-21 09:00")).toEqual(testDate); expect(Planning.stringToDate('2020-03-21 09:00')).toEqual(testDate);
testDate.setFullYear(2020, 0, 31); testDate.setFullYear(2020, 0, 31);
testDate.setHours(18, 30, 0, 0); testDate.setHours(18, 30, 0, 0);
expect(Planning.stringToDate("2020-01-31 18:30")).toEqual(testDate); expect(Planning.stringToDate('2020-01-31 18:30')).toEqual(testDate);
testDate.setFullYear(2020, 50, 50); testDate.setFullYear(2020, 50, 50);
testDate.setHours(65, 65, 0, 0); testDate.setHours(65, 65, 0, 0);
expect(Planning.stringToDate("2020-51-50 65:65")).toEqual(testDate); expect(Planning.stringToDate('2020-51-50 65:65')).toEqual(testDate);
}); });
test('getFormattedEventTime', () => { test('getFormattedEventTime', () => {
expect(Planning.getFormattedEventTime(null, null)) expect(Planning.getFormattedEventTime(null, null)).toBe('/ - /');
.toBe('/ - /'); expect(Planning.getFormattedEventTime(undefined, undefined)).toBe('/ - /');
expect(Planning.getFormattedEventTime(undefined, undefined)) expect(Planning.getFormattedEventTime('20:30', '23:00')).toBe('/ - /');
.toBe('/ - /'); expect(Planning.getFormattedEventTime('2020-03-30', '2020-03-31')).toBe(
expect(Planning.getFormattedEventTime("20:30", "23:00")) '/ - /',
.toBe('/ - /'); );
expect(Planning.getFormattedEventTime("2020-03-30", "2020-03-31"))
.toBe('/ - /');
expect(
expect(Planning.getFormattedEventTime("2020-03-21 09:00", "2020-03-21 09:00")) Planning.getFormattedEventTime('2020-03-21 09:00', '2020-03-21 09:00'),
.toBe('09:00'); ).toBe('09:00');
expect(Planning.getFormattedEventTime("2020-03-21 09:00", "2020-03-22 17:00")) expect(
.toBe('09:00 - 23:59'); Planning.getFormattedEventTime('2020-03-21 09:00', '2020-03-22 17:00'),
expect(Planning.getFormattedEventTime("2020-03-30 20:30", "2020-03-30 23:00")) ).toBe('09:00 - 23:59');
.toBe('20:30 - 23:00'); expect(
Planning.getFormattedEventTime('2020-03-30 20:30', '2020-03-30 23:00'),
).toBe('20:30 - 23:00');
}); });
test('getDateOnlyString', () => { test('getDateOnlyString', () => {
expect(Planning.getDateOnlyString("2020-03-21 09:00")).toBe("2020-03-21"); expect(Planning.getDateOnlyString('2020-03-21 09:00')).toBe('2020-03-21');
expect(Planning.getDateOnlyString("2021-12-15 09:00")).toBe("2021-12-15"); expect(Planning.getDateOnlyString('2021-12-15 09:00')).toBe('2021-12-15');
expect(Planning.getDateOnlyString("2021-12-o5 09:00")).toBeNull(); expect(Planning.getDateOnlyString('2021-12-o5 09:00')).toBeNull();
expect(Planning.getDateOnlyString("2021-12-15 09:")).toBeNull(); expect(Planning.getDateOnlyString('2021-12-15 09:')).toBeNull();
expect(Planning.getDateOnlyString("2021-12-15")).toBeNull(); expect(Planning.getDateOnlyString('2021-12-15')).toBeNull();
expect(Planning.getDateOnlyString("garbage")).toBeNull(); expect(Planning.getDateOnlyString('garbage')).toBeNull();
}); });
test('isEventBefore', () => { test('isEventBefore', () => {
expect(Planning.isEventBefore( expect(
"2020-03-21 09:00", "2020-03-21 10:00")).toBeTrue(); Planning.isEventBefore('2020-03-21 09:00', '2020-03-21 10:00'),
expect(Planning.isEventBefore( ).toBeTrue();
"2020-03-21 10:00", "2020-03-21 10:15")).toBeTrue(); expect(
expect(Planning.isEventBefore( Planning.isEventBefore('2020-03-21 10:00', '2020-03-21 10:15'),
"2020-03-21 10:15", "2021-03-21 10:15")).toBeTrue(); ).toBeTrue();
expect(Planning.isEventBefore( expect(
"2020-03-21 10:15", "2020-05-21 10:15")).toBeTrue(); Planning.isEventBefore('2020-03-21 10:15', '2021-03-21 10:15'),
expect(Planning.isEventBefore( ).toBeTrue();
"2020-03-21 10:15", "2020-03-30 10:15")).toBeTrue(); expect(
Planning.isEventBefore('2020-03-21 10:15', '2020-05-21 10:15'),
).toBeTrue();
expect(
Planning.isEventBefore('2020-03-21 10:15', '2020-03-30 10:15'),
).toBeTrue();
expect(Planning.isEventBefore( expect(
"2020-03-21 10:00", "2020-03-21 10:00")).toBeFalse(); Planning.isEventBefore('2020-03-21 10:00', '2020-03-21 10:00'),
expect(Planning.isEventBefore( ).toBeFalse();
"2020-03-21 10:00", "2020-03-21 09:00")).toBeFalse(); expect(
expect(Planning.isEventBefore( Planning.isEventBefore('2020-03-21 10:00', '2020-03-21 09:00'),
"2020-03-21 10:15", "2020-03-21 10:00")).toBeFalse(); ).toBeFalse();
expect(Planning.isEventBefore( expect(
"2021-03-21 10:15", "2020-03-21 10:15")).toBeFalse(); Planning.isEventBefore('2020-03-21 10:15', '2020-03-21 10:00'),
expect(Planning.isEventBefore( ).toBeFalse();
"2020-05-21 10:15", "2020-03-21 10:15")).toBeFalse(); expect(
expect(Planning.isEventBefore( Planning.isEventBefore('2021-03-21 10:15', '2020-03-21 10:15'),
"2020-03-30 10:15", "2020-03-21 10:15")).toBeFalse(); ).toBeFalse();
expect(
Planning.isEventBefore('2020-05-21 10:15', '2020-03-21 10:15'),
).toBeFalse();
expect(
Planning.isEventBefore('2020-03-30 10:15', '2020-03-21 10:15'),
).toBeFalse();
expect(Planning.isEventBefore( expect(Planning.isEventBefore('garbage', '2020-03-21 10:15')).toBeFalse();
"garbage", "2020-03-21 10:15")).toBeFalse(); expect(Planning.isEventBefore(undefined, undefined)).toBeFalse();
expect(Planning.isEventBefore(
undefined, undefined)).toBeFalse();
}); });
test('dateToString', () => { test('dateToString', () => {
let testDate = new Date(); let testDate = new Date();
testDate.setFullYear(2020, 2, 21); testDate.setFullYear(2020, 2, 21);
testDate.setHours(9, 0, 0, 0); testDate.setHours(9, 0, 0, 0);
expect(Planning.dateToString(testDate)).toBe("2020-03-21 09:00"); expect(Planning.dateToString(testDate)).toBe('2020-03-21 09:00');
testDate.setFullYear(2021, 0, 12); testDate.setFullYear(2021, 0, 12);
testDate.setHours(9, 10, 0, 0); testDate.setHours(9, 10, 0, 0);
expect(Planning.dateToString(testDate)).toBe("2021-01-12 09:10"); expect(Planning.dateToString(testDate)).toBe('2021-01-12 09:10');
testDate.setFullYear(2022, 11, 31); testDate.setFullYear(2022, 11, 31);
testDate.setHours(9, 10, 15, 0); testDate.setHours(9, 10, 15, 0);
expect(Planning.dateToString(testDate)).toBe("2022-12-31 09:10"); expect(Planning.dateToString(testDate)).toBe('2022-12-31 09:10');
}); });
test('generateEmptyCalendar', () => { test('generateEmptyCalendar', () => {
jest.spyOn(Date, 'now') jest
.mockImplementation(() => .spyOn(Date, 'now')
new Date('2020-01-14T00:00:00.000Z').getTime() .mockImplementation(() => new Date('2020-01-14T00:00:00.000Z').getTime());
); let calendar = Planning.generateEmptyCalendar(1);
let calendar = Planning.generateEmptyCalendar(1); expect(calendar).toHaveProperty('2020-01-14');
expect(calendar).toHaveProperty("2020-01-14"); expect(calendar).toHaveProperty('2020-01-20');
expect(calendar).toHaveProperty("2020-01-20"); expect(calendar).toHaveProperty('2020-02-10');
expect(calendar).toHaveProperty("2020-02-10"); expect(Object.keys(calendar).length).toBe(32);
expect(Object.keys(calendar).length).toBe(32); calendar = Planning.generateEmptyCalendar(3);
calendar = Planning.generateEmptyCalendar(3); expect(calendar).toHaveProperty('2020-01-14');
expect(calendar).toHaveProperty("2020-01-14"); expect(calendar).toHaveProperty('2020-01-20');
expect(calendar).toHaveProperty("2020-01-20"); expect(calendar).toHaveProperty('2020-02-10');
expect(calendar).toHaveProperty("2020-02-10"); expect(calendar).toHaveProperty('2020-02-14');
expect(calendar).toHaveProperty("2020-02-14"); expect(calendar).toHaveProperty('2020-03-20');
expect(calendar).toHaveProperty("2020-03-20"); expect(calendar).toHaveProperty('2020-04-12');
expect(calendar).toHaveProperty("2020-04-12"); expect(Object.keys(calendar).length).toBe(92);
expect(Object.keys(calendar).length).toBe(92);
}); });
test('pushEventInOrder', () => { test('pushEventInOrder', () => {
let eventArray = []; let eventArray = [];
let event1 = {date_begin: "2020-01-14 09:15"}; let event1 = {date_begin: '2020-01-14 09:15'};
Planning.pushEventInOrder(eventArray, event1); Planning.pushEventInOrder(eventArray, event1);
expect(eventArray.length).toBe(1); expect(eventArray.length).toBe(1);
expect(eventArray[0]).toBe(event1); expect(eventArray[0]).toBe(event1);
let event2 = {date_begin: "2020-01-14 10:15"}; let event2 = {date_begin: '2020-01-14 10:15'};
Planning.pushEventInOrder(eventArray, event2); Planning.pushEventInOrder(eventArray, event2);
expect(eventArray.length).toBe(2); expect(eventArray.length).toBe(2);
expect(eventArray[0]).toBe(event1); expect(eventArray[0]).toBe(event1);
expect(eventArray[1]).toBe(event2); expect(eventArray[1]).toBe(event2);
let event3 = {date_begin: "2020-01-14 10:15", title: "garbage"}; let event3 = {date_begin: '2020-01-14 10:15', title: 'garbage'};
Planning.pushEventInOrder(eventArray, event3); Planning.pushEventInOrder(eventArray, event3);
expect(eventArray.length).toBe(3); expect(eventArray.length).toBe(3);
expect(eventArray[0]).toBe(event1); expect(eventArray[0]).toBe(event1);
expect(eventArray[1]).toBe(event2); expect(eventArray[1]).toBe(event2);
expect(eventArray[2]).toBe(event3); expect(eventArray[2]).toBe(event3);
let event4 = {date_begin: "2020-01-13 09:00"}; let event4 = {date_begin: '2020-01-13 09:00'};
Planning.pushEventInOrder(eventArray, event4); Planning.pushEventInOrder(eventArray, event4);
expect(eventArray.length).toBe(4); expect(eventArray.length).toBe(4);
expect(eventArray[0]).toBe(event4); expect(eventArray[0]).toBe(event4);
expect(eventArray[1]).toBe(event1); expect(eventArray[1]).toBe(event1);
expect(eventArray[2]).toBe(event2); expect(eventArray[2]).toBe(event2);
expect(eventArray[3]).toBe(event3); expect(eventArray[3]).toBe(event3);
}); });
test('generateEventAgenda', () => { test('generateEventAgenda', () => {
jest.spyOn(Date, 'now') jest
.mockImplementation(() => .spyOn(Date, 'now')
new Date('2020-01-14T00:00:00.000Z').getTime() .mockImplementation(() => new Date('2020-01-14T00:00:00.000Z').getTime());
); let eventList = [
let eventList = [ {date_begin: '2020-01-14 09:15'},
{date_begin: "2020-01-14 09:15"}, {date_begin: '2020-02-01 09:15'},
{date_begin: "2020-02-01 09:15"}, {date_begin: '2020-01-15 09:15'},
{date_begin: "2020-01-15 09:15"}, {date_begin: '2020-02-01 09:30'},
{date_begin: "2020-02-01 09:30"}, {date_begin: '2020-02-01 08:30'},
{date_begin: "2020-02-01 08:30"}, ];
]; const calendar = Planning.generateEventAgenda(eventList, 2);
const calendar = Planning.generateEventAgenda(eventList, 2); expect(calendar['2020-01-14'].length).toBe(1);
expect(calendar["2020-01-14"].length).toBe(1); expect(calendar['2020-01-14'][0]).toBe(eventList[0]);
expect(calendar["2020-01-14"][0]).toBe(eventList[0]); expect(calendar['2020-01-15'].length).toBe(1);
expect(calendar["2020-01-15"].length).toBe(1); expect(calendar['2020-01-15'][0]).toBe(eventList[2]);
expect(calendar["2020-01-15"][0]).toBe(eventList[2]); expect(calendar['2020-02-01'].length).toBe(3);
expect(calendar["2020-02-01"].length).toBe(3); expect(calendar['2020-02-01'][0]).toBe(eventList[4]);
expect(calendar["2020-02-01"][0]).toBe(eventList[4]); expect(calendar['2020-02-01'][1]).toBe(eventList[1]);
expect(calendar["2020-02-01"][1]).toBe(eventList[1]); expect(calendar['2020-02-01'][2]).toBe(eventList[3]);
expect(calendar["2020-02-01"][2]).toBe(eventList[3]);
}); });
test('getCurrentDateString', () => { test('getCurrentDateString', () => {
jest.spyOn(Date, 'now') jest.spyOn(Date, 'now').mockImplementation(() => {
.mockImplementation(() => { let date = new Date();
let date = new Date(); date.setFullYear(2020, 0, 14);
date.setFullYear(2020, 0, 14); date.setHours(15, 30, 54, 65);
date.setHours(15, 30, 54, 65); return date.getTime();
return date.getTime(); });
}); expect(Planning.getCurrentDateString()).toBe('2020-01-14 15:30');
expect(Planning.getCurrentDateString()).toBe('2020-01-14 15:30');
}); });

View file

@ -1,142 +1,167 @@
/* eslint-disable */
import React from 'react'; import React from 'react';
import {getCleanedMachineWatched, getMachineEndDate, getMachineOfId, isMachineWatched} from "../../src/utils/Proxiwash"; import {
getCleanedMachineWatched,
getMachineEndDate,
getMachineOfId,
isMachineWatched,
} from '../../src/utils/Proxiwash';
test('getMachineEndDate', () => { test('getMachineEndDate', () => {
jest.spyOn(Date, 'now') jest
.mockImplementation(() => .spyOn(Date, 'now')
new Date('2020-01-14T15:00:00.000Z').getTime() .mockImplementation(() => new Date('2020-01-14T15:00:00.000Z').getTime());
); let expectDate = new Date('2020-01-14T15:00:00.000Z');
let expectDate = new Date('2020-01-14T15:00:00.000Z'); expectDate.setHours(23);
expectDate.setHours(23); expectDate.setMinutes(10);
expectDate.setMinutes(10); expect(getMachineEndDate({endTime: '23:10'}).getTime()).toBe(
expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime()); expectDate.getTime(),
);
expectDate.setHours(16); expectDate.setHours(16);
expectDate.setMinutes(30); expectDate.setMinutes(30);
expect(getMachineEndDate({endTime: "16:30"}).getTime()).toBe(expectDate.getTime()); expect(getMachineEndDate({endTime: '16:30'}).getTime()).toBe(
expectDate.getTime(),
);
expect(getMachineEndDate({endTime: "15:30"})).toBeNull(); expect(getMachineEndDate({endTime: '15:30'})).toBeNull();
expect(getMachineEndDate({endTime: "13:10"})).toBeNull(); expect(getMachineEndDate({endTime: '13:10'})).toBeNull();
jest.spyOn(Date, 'now') jest
.mockImplementation(() => .spyOn(Date, 'now')
new Date('2020-01-14T23:00:00.000Z').getTime() .mockImplementation(() => new Date('2020-01-14T23:00:00.000Z').getTime());
); expectDate = new Date('2020-01-14T23:00:00.000Z');
expectDate = new Date('2020-01-14T23:00:00.000Z'); expectDate.setHours(0);
expectDate.setHours(0); expectDate.setMinutes(30);
expectDate.setMinutes(30); expect(getMachineEndDate({endTime: '00:30'}).getTime()).toBe(
expect(getMachineEndDate({endTime: "00:30"}).getTime()).toBe(expectDate.getTime()); expectDate.getTime(),
);
}); });
test('isMachineWatched', () => { test('isMachineWatched', () => {
let machineList = [ let machineList = [
{ {
number: "0", number: '0',
endTime: "23:30", endTime: '23:30',
}, },
{ {
number: "1", number: '1',
endTime: "20:30", endTime: '20:30',
}, },
]; ];
expect(isMachineWatched({number: "0", endTime: "23:30"}, machineList)).toBeTrue(); expect(
expect(isMachineWatched({number: "1", endTime: "20:30"}, machineList)).toBeTrue(); isMachineWatched({number: '0', endTime: '23:30'}, machineList),
expect(isMachineWatched({number: "3", endTime: "20:30"}, machineList)).toBeFalse(); ).toBeTrue();
expect(isMachineWatched({number: "1", endTime: "23:30"}, machineList)).toBeFalse(); expect(
isMachineWatched({number: '1', endTime: '20:30'}, machineList),
).toBeTrue();
expect(
isMachineWatched({number: '3', endTime: '20:30'}, machineList),
).toBeFalse();
expect(
isMachineWatched({number: '1', endTime: '23:30'}, machineList),
).toBeFalse();
}); });
test('getMachineOfId', () => { test('getMachineOfId', () => {
let machineList = [ let machineList = [
{ {
number: "0", number: '0',
}, },
{ {
number: "1", number: '1',
}, },
]; ];
expect(getMachineOfId("0", machineList)).toStrictEqual({number: "0"}); expect(getMachineOfId('0', machineList)).toStrictEqual({number: '0'});
expect(getMachineOfId("1", machineList)).toStrictEqual({number: "1"}); expect(getMachineOfId('1', machineList)).toStrictEqual({number: '1'});
expect(getMachineOfId("3", machineList)).toBeNull(); expect(getMachineOfId('3', machineList)).toBeNull();
}); });
test('getCleanedMachineWatched', () => { test('getCleanedMachineWatched', () => {
let machineList = [ let machineList = [
{ {
number: "0", number: '0',
endTime: "23:30", endTime: '23:30',
}, },
{ {
number: "1", number: '1',
endTime: "20:30", endTime: '20:30',
}, },
{ {
number: "2", number: '2',
endTime: "", endTime: '',
}, },
]; ];
let watchList = [ let watchList = [
{ {
number: "0", number: '0',
endTime: "23:30", endTime: '23:30',
}, },
{ {
number: "1", number: '1',
endTime: "20:30", endTime: '20:30',
}, },
{ {
number: "2", number: '2',
endTime: "", endTime: '',
}, },
]; ];
let cleanedList = watchList; let cleanedList = watchList;
expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(
cleanedList,
);
watchList = [ watchList = [
{ {
number: "0", number: '0',
endTime: "23:30", endTime: '23:30',
}, },
{ {
number: "1", number: '1',
endTime: "20:30", endTime: '20:30',
}, },
{ {
number: "2", number: '2',
endTime: "15:30", endTime: '15:30',
}, },
]; ];
cleanedList = [ cleanedList = [
{ {
number: "0", number: '0',
endTime: "23:30", endTime: '23:30',
}, },
{ {
number: "1", number: '1',
endTime: "20:30", endTime: '20:30',
}, },
]; ];
expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(
cleanedList,
);
watchList = [ watchList = [
{ {
number: "0", number: '0',
endTime: "23:30", endTime: '23:30',
}, },
{ {
number: "1", number: '1',
endTime: "20:31", endTime: '20:31',
}, },
{ {
number: "3", number: '3',
endTime: "15:30", endTime: '15:30',
}, },
]; ];
cleanedList = [ cleanedList = [
{ {
number: "0", number: '0',
endTime: "23:30", endTime: '23:30',
}, },
]; ];
expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(
cleanedList,
);
}); });

View file

@ -1,45 +1,47 @@
import React from 'react'; /* eslint-disable */
import {isResponseValid} from "../../src/utils/WebData";
let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native import React from 'react';
import {isApiResponseValid} from '../../src/utils/WebData';
const fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
test('isRequestResponseValid', () => { test('isRequestResponseValid', () => {
let json = { let json = {
error: 0, error: 0,
data: {} data: {},
}; };
expect(isResponseValid(json)).toBeTrue(); expect(isApiResponseValid(json)).toBeTrue();
json = { json = {
error: 1, error: 1,
data: {} data: {},
}; };
expect(isResponseValid(json)).toBeTrue(); expect(isApiResponseValid(json)).toBeTrue();
json = { json = {
error: 50, error: 50,
data: {} data: {},
}; };
expect(isResponseValid(json)).toBeTrue(); expect(isApiResponseValid(json)).toBeTrue();
json = { json = {
error: 50, error: 50,
data: {truc: 'machin'} data: {truc: 'machin'},
}; };
expect(isResponseValid(json)).toBeTrue(); expect(isApiResponseValid(json)).toBeTrue();
json = { json = {
message: 'coucou' message: 'coucou',
}; };
expect(isResponseValid(json)).toBeFalse(); expect(isApiResponseValid(json)).toBeFalse();
json = { json = {
error: 'coucou', error: 'coucou',
data: {truc: 'machin'} data: {truc: 'machin'},
}; };
expect(isResponseValid(json)).toBeFalse(); expect(isApiResponseValid(json)).toBeFalse();
json = { json = {
error: 0, error: 0,
data: 'coucou' data: 'coucou',
}; };
expect(isResponseValid(json)).toBeFalse(); expect(isApiResponseValid(json)).toBeFalse();
json = { json = {
error: 0, error: 0,
}; };
expect(isResponseValid(json)).toBeFalse(); expect(isApiResponseValid(json)).toBeFalse();
}); });

View file

@ -1,103 +1,106 @@
/* eslint-disable */
import React from 'react'; import React from 'react';
import GridManager from "../logic/GridManager"; import GridManager from '../logic/GridManager';
import ScoreManager from "../logic/ScoreManager"; import ScoreManager from '../logic/ScoreManager';
import Piece from "../logic/Piece"; import Piece from '../logic/Piece';
let colors = { let colors = {
tetrisBackground: "#000002" tetrisBackground: '#000002',
}; };
jest.mock("../ScoreManager"); jest.mock('../ScoreManager');
afterAll(() => { afterAll(() => {
jest.restoreAllMocks(); jest.restoreAllMocks();
}); });
test('getEmptyLine', () => { test('getEmptyLine', () => {
let g = new GridManager(2, 2, colors); let g = new GridManager(2, 2, colors);
expect(g.getEmptyLine(2)).toStrictEqual([ expect(g.getEmptyLine(2)).toStrictEqual([
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
]); ]);
expect(g.getEmptyLine(-1)).toStrictEqual([]); expect(g.getEmptyLine(-1)).toStrictEqual([]);
}); });
test('getEmptyGrid', () => { test('getEmptyGrid', () => {
let g = new GridManager(2, 2, colors); let g = new GridManager(2, 2, colors);
expect(g.getEmptyGrid(2, 2)).toStrictEqual([ expect(g.getEmptyGrid(2, 2)).toStrictEqual([
[ [
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
], ],
[ [
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
], ],
]); ]);
expect(g.getEmptyGrid(-1, 2)).toStrictEqual([]); expect(g.getEmptyGrid(-1, 2)).toStrictEqual([]);
expect(g.getEmptyGrid(2, -1)).toStrictEqual([[], []]); expect(g.getEmptyGrid(2, -1)).toStrictEqual([[], []]);
}); });
test('getLinesToClear', () => { test('getLinesToClear', () => {
let g = new GridManager(2, 2, colors); let g = new GridManager(2, 2, colors);
g.getCurrentGrid()[0][0].isEmpty = false; g.getCurrentGrid()[0][0].isEmpty = false;
g.getCurrentGrid()[0][1].isEmpty = false; g.getCurrentGrid()[0][1].isEmpty = false;
let coord = [{x: 1, y: 0}]; let coord = [{x: 1, y: 0}];
expect(g.getLinesToClear(coord)).toStrictEqual([0]); expect(g.getLinesToClear(coord)).toStrictEqual([0]);
g.getCurrentGrid()[0][0].isEmpty = true; g.getCurrentGrid()[0][0].isEmpty = true;
g.getCurrentGrid()[0][1].isEmpty = true; g.getCurrentGrid()[0][1].isEmpty = true;
g.getCurrentGrid()[1][0].isEmpty = false; g.getCurrentGrid()[1][0].isEmpty = false;
g.getCurrentGrid()[1][1].isEmpty = false; g.getCurrentGrid()[1][1].isEmpty = false;
expect(g.getLinesToClear(coord)).toStrictEqual([]); expect(g.getLinesToClear(coord)).toStrictEqual([]);
coord = [{x: 1, y: 1}]; coord = [{x: 1, y: 1}];
expect(g.getLinesToClear(coord)).toStrictEqual([1]); expect(g.getLinesToClear(coord)).toStrictEqual([1]);
}); });
test('clearLines', () => { test('clearLines', () => {
let g = new GridManager(2, 2, colors); let g = new GridManager(2, 2, colors);
let grid = [ let grid = [
[ [
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
], ],
[ [
{color: '0', isEmpty: true}, {color: '0', isEmpty: true},
{color: '0', isEmpty: true}, {color: '0', isEmpty: true},
], ],
]; ];
g.getCurrentGrid()[1][0].color = '0'; g.getCurrentGrid()[1][0].color = '0';
g.getCurrentGrid()[1][1].color = '0'; g.getCurrentGrid()[1][1].color = '0';
expect(g.getCurrentGrid()).toStrictEqual(grid); expect(g.getCurrentGrid()).toStrictEqual(grid);
let scoreManager = new ScoreManager(); let scoreManager = new ScoreManager();
g.clearLines([1], scoreManager); g.clearLines([1], scoreManager);
grid = [ grid = [
[ [
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
], ],
[ [
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
], ],
]; ];
expect(g.getCurrentGrid()).toStrictEqual(grid); expect(g.getCurrentGrid()).toStrictEqual(grid);
}); });
test('freezeTetromino', () => { test('freezeTetromino', () => {
let g = new GridManager(2, 2, colors); let g = new GridManager(2, 2, colors);
let spy1 = jest.spyOn(GridManager.prototype, 'getLinesToClear') let spy1 = jest
.mockImplementation(() => {}); .spyOn(GridManager.prototype, 'getLinesToClear')
let spy2 = jest.spyOn(GridManager.prototype, 'clearLines') .mockImplementation(() => {});
.mockImplementation(() => {}); let spy2 = jest
g.freezeTetromino(new Piece({}), null); .spyOn(GridManager.prototype, 'clearLines')
.mockImplementation(() => {});
g.freezeTetromino(new Piece({}), null);
expect(spy1).toHaveBeenCalled(); expect(spy1).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled(); expect(spy2).toHaveBeenCalled();
spy1.mockRestore(); spy1.mockRestore();
spy2.mockRestore(); spy2.mockRestore();
}); });

View file

@ -1,155 +1,186 @@
/* eslint-disable */
import React from 'react'; import React from 'react';
import Piece from "../logic/Piece"; import Piece from '../logic/Piece';
import ShapeI from "../Shapes/ShapeI"; import ShapeI from '../Shapes/ShapeI';
let colors = { let colors = {
tetrisI: "#000001", tetrisI: '#000001',
tetrisBackground: "#000002" tetrisBackground: '#000002',
}; };
jest.mock("../Shapes/ShapeI"); jest.mock('../Shapes/ShapeI');
beforeAll(() => { beforeAll(() => {
jest.spyOn(Piece.prototype, 'getRandomShape') jest
.mockImplementation((colors: Object) => {return new ShapeI(colors);}); .spyOn(Piece.prototype, 'getRandomShape')
.mockImplementation((colors: Object) => {
return new ShapeI(colors);
});
}); });
afterAll(() => { afterAll(() => {
jest.restoreAllMocks(); jest.restoreAllMocks();
}); });
test('isPositionValid', () => { test('isPositionValid', () => {
let x = 0; let x = 0;
let y = 0; let y = 0;
let spy = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates') let spy = jest
.mockImplementation(() => {return [{x: x, y: y}];}); .spyOn(ShapeI.prototype, 'getCellsCoordinates')
let grid = [ .mockImplementation(() => {
[{isEmpty: true}, {isEmpty: true}], return [{x: x, y: y}];
[{isEmpty: true}, {isEmpty: false}], });
]; let grid = [
let size = 2; [{isEmpty: true}, {isEmpty: true}],
[{isEmpty: true}, {isEmpty: false}],
];
let size = 2;
let p = new Piece(colors); let p = new Piece(colors);
expect(p.isPositionValid(grid, size, size)).toBeTrue(); expect(p.isPositionValid(grid, size, size)).toBeTrue();
x = 1; y = 0; x = 1;
expect(p.isPositionValid(grid, size, size)).toBeTrue(); y = 0;
x = 0; y = 1; expect(p.isPositionValid(grid, size, size)).toBeTrue();
expect(p.isPositionValid(grid, size, size)).toBeTrue(); x = 0;
x = 1; y = 1; y = 1;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); expect(p.isPositionValid(grid, size, size)).toBeTrue();
x = 2; y = 0; x = 1;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); y = 1;
x = -1; y = 0; expect(p.isPositionValid(grid, size, size)).toBeFalse();
expect(p.isPositionValid(grid, size, size)).toBeFalse(); x = 2;
x = 0; y = 2; y = 0;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); expect(p.isPositionValid(grid, size, size)).toBeFalse();
x = 0; y = -1; x = -1;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); y = 0;
expect(p.isPositionValid(grid, size, size)).toBeFalse();
x = 0;
y = 2;
expect(p.isPositionValid(grid, size, size)).toBeFalse();
x = 0;
y = -1;
expect(p.isPositionValid(grid, size, size)).toBeFalse();
spy.mockRestore(); spy.mockRestore();
}); });
test('tryMove', () => { test('tryMove', () => {
let p = new Piece(colors); let p = new Piece(colors);
const callbackMock = jest.fn(); const callbackMock = jest.fn();
let isValid = true; let isValid = true;
let spy1 = jest.spyOn(Piece.prototype, 'isPositionValid') let spy1 = jest
.mockImplementation(() => {return isValid;}); .spyOn(Piece.prototype, 'isPositionValid')
let spy2 = jest.spyOn(Piece.prototype, 'removeFromGrid') .mockImplementation(() => {
.mockImplementation(() => {}); return isValid;
let spy3 = jest.spyOn(Piece.prototype, 'toGrid') });
.mockImplementation(() => {}); let spy2 = jest
.spyOn(Piece.prototype, 'removeFromGrid')
.mockImplementation(() => {});
let spy3 = jest.spyOn(Piece.prototype, 'toGrid').mockImplementation(() => {});
expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeTrue(); expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeTrue();
isValid = false; isValid = false;
expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse(); expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse();
isValid = true; isValid = true;
expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue(); expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue();
expect(callbackMock).toBeCalledTimes(0); expect(callbackMock).toBeCalledTimes(0);
isValid = false; isValid = false;
expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse(); expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse();
expect(callbackMock).toBeCalledTimes(1); expect(callbackMock).toBeCalledTimes(1);
expect(spy2).toBeCalledTimes(4); expect(spy2).toBeCalledTimes(4);
expect(spy3).toBeCalledTimes(4); expect(spy3).toBeCalledTimes(4);
spy1.mockRestore(); spy1.mockRestore();
spy2.mockRestore(); spy2.mockRestore();
spy3.mockRestore(); spy3.mockRestore();
}); });
test('tryRotate', () => { test('tryRotate', () => {
let p = new Piece(colors); let p = new Piece(colors);
let isValid = true; let isValid = true;
let spy1 = jest.spyOn(Piece.prototype, 'isPositionValid') let spy1 = jest
.mockImplementation(() => {return isValid;}); .spyOn(Piece.prototype, 'isPositionValid')
let spy2 = jest.spyOn(Piece.prototype, 'removeFromGrid') .mockImplementation(() => {
.mockImplementation(() => {}); return isValid;
let spy3 = jest.spyOn(Piece.prototype, 'toGrid') });
.mockImplementation(() => {}); let spy2 = jest
.spyOn(Piece.prototype, 'removeFromGrid')
.mockImplementation(() => {});
let spy3 = jest.spyOn(Piece.prototype, 'toGrid').mockImplementation(() => {});
expect(p.tryRotate( null, null, null)).toBeTrue(); expect(p.tryRotate(null, null, null)).toBeTrue();
isValid = false; isValid = false;
expect(p.tryRotate( null, null, null)).toBeFalse(); expect(p.tryRotate(null, null, null)).toBeFalse();
expect(spy2).toBeCalledTimes(2); expect(spy2).toBeCalledTimes(2);
expect(spy3).toBeCalledTimes(2); expect(spy3).toBeCalledTimes(2);
spy1.mockRestore(); spy1.mockRestore();
spy2.mockRestore(); spy2.mockRestore();
spy3.mockRestore(); spy3.mockRestore();
}); });
test('toGrid', () => { test('toGrid', () => {
let x = 0; let x = 0;
let y = 0; let y = 0;
let spy1 = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates') let spy1 = jest
.mockImplementation(() => {return [{x: x, y: y}];}); .spyOn(ShapeI.prototype, 'getCellsCoordinates')
let spy2 = jest.spyOn(ShapeI.prototype, 'getColor') .mockImplementation(() => {
.mockImplementation(() => {return colors.tetrisI;}); return [{x: x, y: y}];
let grid = [ });
[{isEmpty: true}, {isEmpty: true}], let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
[{isEmpty: true}, {isEmpty: true}], return colors.tetrisI;
]; });
let expectedGrid = [ let grid = [
[{color: colors.tetrisI, isEmpty: false}, {isEmpty: true}], [{isEmpty: true}, {isEmpty: true}],
[{isEmpty: true}, {isEmpty: true}], [{isEmpty: true}, {isEmpty: true}],
]; ];
let expectedGrid = [
[{color: colors.tetrisI, isEmpty: false}, {isEmpty: true}],
[{isEmpty: true}, {isEmpty: true}],
];
let p = new Piece(colors); let p = new Piece(colors);
p.toGrid(grid, true); p.toGrid(grid, true);
expect(grid).toStrictEqual(expectedGrid); expect(grid).toStrictEqual(expectedGrid);
spy1.mockRestore(); spy1.mockRestore();
spy2.mockRestore(); spy2.mockRestore();
}); });
test('removeFromGrid', () => { test('removeFromGrid', () => {
let gridOld = [ let gridOld = [
[ [
{color: colors.tetrisI, isEmpty: false}, {color: colors.tetrisI, isEmpty: false},
{color: colors.tetrisI, isEmpty: false}, {color: colors.tetrisI, isEmpty: false},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
], ],
]; ];
let gridNew = [ let gridNew = [
[ [
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
{color: colors.tetrisBackground, isEmpty: true}, {color: colors.tetrisBackground, isEmpty: true},
], ],
]; ];
let oldCoord = [{x: 0, y: 0}, {x: 1, y: 0}]; let oldCoord = [
let spy1 = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates') {x: 0, y: 0},
.mockImplementation(() => {return oldCoord;}); {x: 1, y: 0},
let spy2 = jest.spyOn(ShapeI.prototype, 'getColor') ];
.mockImplementation(() => {return colors.tetrisI;}); let spy1 = jest
let p = new Piece(colors); .spyOn(ShapeI.prototype, 'getCellsCoordinates')
p.removeFromGrid(gridOld); .mockImplementation(() => {
expect(gridOld).toStrictEqual(gridNew); return oldCoord;
});
let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
return colors.tetrisI;
});
let p = new Piece(colors);
p.removeFromGrid(gridOld);
expect(gridOld).toStrictEqual(gridNew);
spy1.mockRestore(); spy1.mockRestore();
spy2.mockRestore(); spy2.mockRestore();
}); });

View file

@ -1,71 +1,72 @@
import React from 'react'; /* eslint-disable */
import ScoreManager from "../logic/ScoreManager";
import React from 'react';
import ScoreManager from '../logic/ScoreManager';
test('incrementScore', () => { test('incrementScore', () => {
let scoreManager = new ScoreManager(); let scoreManager = new ScoreManager();
expect(scoreManager.getScore()).toBe(0); expect(scoreManager.getScore()).toBe(0);
scoreManager.incrementScore(); scoreManager.incrementScore();
expect(scoreManager.getScore()).toBe(1); expect(scoreManager.getScore()).toBe(1);
}); });
test('addLinesRemovedPoints', () => { test('addLinesRemovedPoints', () => {
let scoreManager = new ScoreManager(); let scoreManager = new ScoreManager();
scoreManager.addLinesRemovedPoints(0); scoreManager.addLinesRemovedPoints(0);
scoreManager.addLinesRemovedPoints(5); scoreManager.addLinesRemovedPoints(5);
expect(scoreManager.getScore()).toBe(0); expect(scoreManager.getScore()).toBe(0);
expect(scoreManager.getLevelProgression()).toBe(0); expect(scoreManager.getLevelProgression()).toBe(0);
scoreManager.addLinesRemovedPoints(1); scoreManager.addLinesRemovedPoints(1);
expect(scoreManager.getScore()).toBe(40); expect(scoreManager.getScore()).toBe(40);
expect(scoreManager.getLevelProgression()).toBe(1); expect(scoreManager.getLevelProgression()).toBe(1);
scoreManager.addLinesRemovedPoints(2); scoreManager.addLinesRemovedPoints(2);
expect(scoreManager.getScore()).toBe(140); expect(scoreManager.getScore()).toBe(140);
expect(scoreManager.getLevelProgression()).toBe(4); expect(scoreManager.getLevelProgression()).toBe(4);
scoreManager.addLinesRemovedPoints(3); scoreManager.addLinesRemovedPoints(3);
expect(scoreManager.getScore()).toBe(440); expect(scoreManager.getScore()).toBe(440);
expect(scoreManager.getLevelProgression()).toBe(9); expect(scoreManager.getLevelProgression()).toBe(9);
scoreManager.addLinesRemovedPoints(4); scoreManager.addLinesRemovedPoints(4);
expect(scoreManager.getScore()).toBe(1640); expect(scoreManager.getScore()).toBe(1640);
expect(scoreManager.getLevelProgression()).toBe(17); expect(scoreManager.getLevelProgression()).toBe(17);
}); });
test('canLevelUp', () => { test('canLevelUp', () => {
let scoreManager = new ScoreManager(); let scoreManager = new ScoreManager();
expect(scoreManager.canLevelUp()).toBeFalse(); expect(scoreManager.canLevelUp()).toBeFalse();
expect(scoreManager.getLevel()).toBe(0); expect(scoreManager.getLevel()).toBe(0);
expect(scoreManager.getLevelProgression()).toBe(0); expect(scoreManager.getLevelProgression()).toBe(0);
scoreManager.addLinesRemovedPoints(1); scoreManager.addLinesRemovedPoints(1);
expect(scoreManager.canLevelUp()).toBeTrue(); expect(scoreManager.canLevelUp()).toBeTrue();
expect(scoreManager.getLevel()).toBe(1); expect(scoreManager.getLevel()).toBe(1);
expect(scoreManager.getLevelProgression()).toBe(1); expect(scoreManager.getLevelProgression()).toBe(1);
scoreManager.addLinesRemovedPoints(1); scoreManager.addLinesRemovedPoints(1);
expect(scoreManager.canLevelUp()).toBeFalse(); expect(scoreManager.canLevelUp()).toBeFalse();
expect(scoreManager.getLevel()).toBe(1); expect(scoreManager.getLevel()).toBe(1);
expect(scoreManager.getLevelProgression()).toBe(2); expect(scoreManager.getLevelProgression()).toBe(2);
scoreManager.addLinesRemovedPoints(2); scoreManager.addLinesRemovedPoints(2);
expect(scoreManager.canLevelUp()).toBeFalse(); expect(scoreManager.canLevelUp()).toBeFalse();
expect(scoreManager.getLevel()).toBe(1); expect(scoreManager.getLevel()).toBe(1);
expect(scoreManager.getLevelProgression()).toBe(5); expect(scoreManager.getLevelProgression()).toBe(5);
scoreManager.addLinesRemovedPoints(1); scoreManager.addLinesRemovedPoints(1);
expect(scoreManager.canLevelUp()).toBeTrue(); expect(scoreManager.canLevelUp()).toBeTrue();
expect(scoreManager.getLevel()).toBe(2); expect(scoreManager.getLevel()).toBe(2);
expect(scoreManager.getLevelProgression()).toBe(1); expect(scoreManager.getLevelProgression()).toBe(1);
scoreManager.addLinesRemovedPoints(4); scoreManager.addLinesRemovedPoints(4);
expect(scoreManager.canLevelUp()).toBeFalse(); expect(scoreManager.canLevelUp()).toBeFalse();
expect(scoreManager.getLevel()).toBe(2); expect(scoreManager.getLevel()).toBe(2);
expect(scoreManager.getLevelProgression()).toBe(9); expect(scoreManager.getLevelProgression()).toBe(9);
scoreManager.addLinesRemovedPoints(2); scoreManager.addLinesRemovedPoints(2);
expect(scoreManager.canLevelUp()).toBeTrue(); expect(scoreManager.canLevelUp()).toBeTrue();
expect(scoreManager.getLevel()).toBe(3); expect(scoreManager.getLevel()).toBe(3);
expect(scoreManager.getLevelProgression()).toBe(2); expect(scoreManager.getLevelProgression()).toBe(2);
}); });

View file

@ -1,104 +1,106 @@
/* eslint-disable */
import React from 'react'; import React from 'react';
import BaseShape from "../Shapes/BaseShape"; import BaseShape from '../Shapes/BaseShape';
import ShapeI from "../Shapes/ShapeI"; import ShapeI from '../Shapes/ShapeI';
const colors = { const colors = {
tetrisI: '#000001', tetrisI: '#000001',
tetrisO: '#000002', tetrisO: '#000002',
tetrisT: '#000003', tetrisT: '#000003',
tetrisS: '#000004', tetrisS: '#000004',
tetrisZ: '#000005', tetrisZ: '#000005',
tetrisJ: '#000006', tetrisJ: '#000006',
tetrisL: '#000007', tetrisL: '#000007',
}; };
test('constructor', () => { test('constructor', () => {
expect(() => new BaseShape()).toThrow(Error); expect(() => new BaseShape()).toThrow(Error);
let T = new ShapeI(colors); let T = new ShapeI(colors);
expect(T.position.y).toBe(0); expect(T.position.y).toBe(0);
expect(T.position.x).toBe(3); expect(T.position.x).toBe(3);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]);
expect(T.getColor()).toBe(colors.tetrisI); expect(T.getColor()).toBe(colors.tetrisI);
}); });
test("move", () => { test('move', () => {
let T = new ShapeI(colors); let T = new ShapeI(colors);
T.move(0, 1); T.move(0, 1);
expect(T.position.x).toBe(3); expect(T.position.x).toBe(3);
expect(T.position.y).toBe(1); expect(T.position.y).toBe(1);
T.move(1, 0); T.move(1, 0);
expect(T.position.x).toBe(4); expect(T.position.x).toBe(4);
expect(T.position.y).toBe(1); expect(T.position.y).toBe(1);
T.move(1, 1); T.move(1, 1);
expect(T.position.x).toBe(5); expect(T.position.x).toBe(5);
expect(T.position.y).toBe(2); expect(T.position.y).toBe(2);
T.move(2, 2); T.move(2, 2);
expect(T.position.x).toBe(7); expect(T.position.x).toBe(7);
expect(T.position.y).toBe(4); expect(T.position.y).toBe(4);
T.move(-1, -1); T.move(-1, -1);
expect(T.position.x).toBe(6); expect(T.position.x).toBe(6);
expect(T.position.y).toBe(3); expect(T.position.y).toBe(3);
}); });
test('rotate', () => { test('rotate', () => {
let T = new ShapeI(colors); let T = new ShapeI(colors);
T.rotate(true); T.rotate(true);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]);
T.rotate(true); T.rotate(true);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[2]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[2]);
T.rotate(true); T.rotate(true);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[3]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[3]);
T.rotate(true); T.rotate(true);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]);
T.rotate(false); T.rotate(false);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[3]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[3]);
T.rotate(false); T.rotate(false);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[2]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[2]);
T.rotate(false); T.rotate(false);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]);
T.rotate(false); T.rotate(false);
expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]);
}); });
test('getCellsCoordinates', () => { test('getCellsCoordinates', () => {
let T = new ShapeI(colors); let T = new ShapeI(colors);
expect(T.getCellsCoordinates(false)).toStrictEqual([ expect(T.getCellsCoordinates(false)).toStrictEqual([
{x: 0, y: 1}, {x: 0, y: 1},
{x: 1, y: 1}, {x: 1, y: 1},
{x: 2, y: 1}, {x: 2, y: 1},
{x: 3, y: 1}, {x: 3, y: 1},
]); ]);
expect(T.getCellsCoordinates(true)).toStrictEqual([ expect(T.getCellsCoordinates(true)).toStrictEqual([
{x: 3, y: 1}, {x: 3, y: 1},
{x: 4, y: 1}, {x: 4, y: 1},
{x: 5, y: 1}, {x: 5, y: 1},
{x: 6, y: 1}, {x: 6, y: 1},
]); ]);
T.move(1, 1); T.move(1, 1);
expect(T.getCellsCoordinates(false)).toStrictEqual([ expect(T.getCellsCoordinates(false)).toStrictEqual([
{x: 0, y: 1}, {x: 0, y: 1},
{x: 1, y: 1}, {x: 1, y: 1},
{x: 2, y: 1}, {x: 2, y: 1},
{x: 3, y: 1}, {x: 3, y: 1},
]); ]);
expect(T.getCellsCoordinates(true)).toStrictEqual([ expect(T.getCellsCoordinates(true)).toStrictEqual([
{x: 4, y: 2}, {x: 4, y: 2},
{x: 5, y: 2}, {x: 5, y: 2},
{x: 6, y: 2}, {x: 6, y: 2},
{x: 7, y: 2}, {x: 7, y: 2},
]); ]);
T.rotate(true); T.rotate(true);
expect(T.getCellsCoordinates(false)).toStrictEqual([ expect(T.getCellsCoordinates(false)).toStrictEqual([
{x: 2, y: 0}, {x: 2, y: 0},
{x: 2, y: 1}, {x: 2, y: 1},
{x: 2, y: 2}, {x: 2, y: 2},
{x: 2, y: 3}, {x: 2, y: 3},
]); ]);
expect(T.getCellsCoordinates(true)).toStrictEqual([ expect(T.getCellsCoordinates(true)).toStrictEqual([
{x: 6, y: 1}, {x: 6, y: 1},
{x: 6, y: 2}, {x: 6, y: 2},
{x: 6, y: 3}, {x: 6, y: 3},
{x: 6, y: 4}, {x: 6, y: 4},
]); ]);
}); });