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,10 +1,12 @@
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();
@ -13,132 +15,124 @@ afterEach(() => {
}); });
test('isLoggedIn yes', () => { test('isLoggedIn yes', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
.spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return 'token'; 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
.spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return null; return null;
}); });
return expect(c.isLoggedIn()).toBe(false); return expect(c.isLoggedIn()).toBe(false);
}); });
test("isConnectionResponseValid", () => { test('connect bad credentials', () => {
let json = {
error: 0,
data: {token: 'token'}
};
expect(c.isConnectionResponseValid(json)).toBeTrue();
json = {
error: 2,
data: {}
};
expect(c.isConnectionResponseValid(json)).toBeTrue();
json = {
error: 0,
data: {token: ''}
};
expect(c.isConnectionResponseValid(json)).toBeFalse();
json = {
error: 'prout',
data: {token: ''}
};
expect(c.isConnectionResponseValid(json)).toBeFalse();
});
test("connect bad 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.BAD_CREDENTIALS,
data: {} data: {},
}; };
}, },
})
}); });
return expect(c.connect('email', 'password')) });
.rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS); return expect(c.connect('email', 'password')).rejects.toBe(
ERROR_TYPE.BAD_CREDENTIALS,
);
}); });
test("connect good 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.SUCCESS, error: ERROR_TYPE.SUCCESS,
data: {token: 'token'} data: {token: 'token'},
}; };
}, },
})
}); });
jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => { });
jest
.spyOn(ConnectionManager.prototype, 'saveLogin')
.mockImplementationOnce(() => {
return Promise.resolve(true); return Promise.resolve(true);
}); });
return expect(c.connect('email', 'password')).resolves.toBeTruthy(); return expect(c.connect('email', 'password')).resolves.toBeTruthy();
}); });
test("connect good credentials no consent", () => { 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.NO_CONSENT, error: ERROR_TYPE.NO_CONSENT,
data: {} data: {},
}; };
}, },
})
}); });
return expect(c.connect('email', 'password')) });
.rejects.toBe(ERROR_TYPE.NO_CONSENT); return expect(c.connect('email', 'password')).rejects.toBe(
ERROR_TYPE.NO_CONSENT,
);
}); });
test("connect good credentials, fail save token", () => { 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.SUCCESS, error: ERROR_TYPE.SUCCESS,
data: {token: 'token'} data: {token: 'token'},
}; };
}, },
})
}); });
jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => { });
jest
.spyOn(ConnectionManager.prototype, 'saveLogin')
.mockImplementationOnce(() => {
return Promise.reject(false); return Promise.reject(false);
}); });
return expect(c.connect('email', 'password')).rejects.toBe(ERROR_TYPE.UNKNOWN); return expect(c.connect('email', 'password')).rejects.toBe(
ERROR_TYPE.UNKNOWN,
);
}); });
test("connect connection error", () => { test('connect connection error', () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.reject(); return Promise.reject();
}); });
return expect(c.connect('email', 'password')) return expect(c.connect('email', 'password')).rejects.toBe(
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); ERROR_TYPE.CONNECTION_ERROR,
);
}); });
test("connect bogus response 1", () => { test('connect bogus response 1', () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
json: () => { json: () => {
return { return {
thing: true, thing: true,
wrong: '', 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('authenticatedRequest success', () => {
test("authenticatedRequest success", () => { jest
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { .spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return 'token'; return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
@ -146,17 +140,20 @@ test("authenticatedRequest success", () => {
json: () => { json: () => {
return { return {
error: ERROR_TYPE.SUCCESS, error: ERROR_TYPE.SUCCESS,
data: {coucou: 'toi'} data: {coucou: 'toi'},
}; };
}, },
})
}); });
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'),
).resolves.toStrictEqual({coucou: 'toi'});
}); });
test("authenticatedRequest error wrong token", () => { test('authenticatedRequest error wrong token', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
.spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return 'token'; return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
@ -164,17 +161,20 @@ test("authenticatedRequest error wrong token", () => {
json: () => { json: () => {
return { return {
error: ERROR_TYPE.BAD_TOKEN, error: ERROR_TYPE.BAD_TOKEN,
data: {} 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.BAD_TOKEN);
}); });
test("authenticatedRequest error bogus response", () => { test('authenticatedRequest error bogus response', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
.spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return 'token'; return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
@ -184,27 +184,34 @@ test("authenticatedRequest error bogus response", () => {
error: ERROR_TYPE.SUCCESS, error: ERROR_TYPE.SUCCESS,
}; };
}, },
})
}); });
return expect(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 connection error', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
.spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return 'token'; return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.reject() return Promise.reject();
}); });
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check')) return expect(
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'),
).rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
}); });
test("authenticatedRequest error no token", () => { test('authenticatedRequest error no token', () => {
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { jest
.spyOn(ConnectionManager.prototype, 'getToken')
.mockImplementationOnce(() => {
return null; return null;
}); });
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check')) return expect(
.rejects.toBe(ERROR_TYPE.UNKNOWN); 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(
new Date('2020-01-14').getTime(),
); );
expect(EquipmentBooking.getCurrentDay().getTime()).toBe(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(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-11").getTime()); expect(
testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-09"}]; EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime(),
expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-10").getTime()); ).toBe(new Date('2020-07-11').getTime());
testDevice.booked_at = [{begin: '2020-07-07', end: '2020-07-09'}];
expect(
EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime(),
).toBe(new Date('2020-07-10').getTime());
testDevice.booked_at = [ testDevice.booked_at = [
{begin: "2020-07-07", end: "2020-07-09"}, {begin: '2020-07-07', end: '2020-07-09'},
{begin: "2020-07-10", end: "2020-07-16"}, {begin: '2020-07-10', end: '2020-07-16'},
]; ];
expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-17").getTime()); expect(
EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime(),
).toBe(new Date('2020-07-17').getTime());
testDevice.booked_at = [ testDevice.booked_at = [
{begin: "2020-07-07", end: "2020-07-09"}, {begin: '2020-07-07', end: '2020-07-09'},
{begin: "2020-07-10", end: "2020-07-12"}, {begin: '2020-07-10', end: '2020-07-12'},
{begin: "2020-07-14", end: "2020-07-16"}, {begin: '2020-07-14', end: '2020-07-16'},
]; ];
expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-13").getTime()); 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) => {
const prefix = 'screens.equipment.';
if (translationString === prefix + 'otherYear') return '0';
else if (translationString === prefix + 'otherMonth') return '1';
else if (translationString === prefix + 'thisMonth') return '2';
else if (translationString === prefix + 'tomorrow') return '3';
else if (translationString === prefix + 'today') return '4';
else return null;
});
expect(EquipmentBooking.getRelativeDateString(new Date('2020-07-09'))).toBe(
'4',
); );
jest.spyOn(i18n, 't') expect(EquipmentBooking.getRelativeDateString(new Date('2020-07-10'))).toBe(
.mockImplementation((translationString: string) => { '3',
const prefix = "screens.equipment."; );
if (translationString === prefix + "otherYear") expect(EquipmentBooking.getRelativeDateString(new Date('2020-07-11'))).toBe(
return "0"; '2',
else if (translationString === prefix + "otherMonth") );
return "1"; expect(EquipmentBooking.getRelativeDateString(new Date('2020-07-30'))).toBe(
else if (translationString === prefix + "thisMonth") '2',
return "2"; );
else if (translationString === prefix + "tomorrow") expect(EquipmentBooking.getRelativeDateString(new Date('2020-08-30'))).toBe(
return "3"; '1',
else if (translationString === prefix + "today") );
return "4"; expect(EquipmentBooking.getRelativeDateString(new Date('2020-11-10'))).toBe(
else '1',
return null; );
} expect(EquipmentBooking.getRelativeDateString(new Date('2021-11-10'))).toBe(
'0',
); );
expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-09"))).toBe("4");
expect(EquipmentBooking.getRelativeDateString(new Date("2020-07-10"))).toBe("3");
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("1");
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(
result,
);
testDevice.booked_at = [ testDevice.booked_at = [
{begin: "2020-07-07", end: "2020-07-10"}, {begin: '2020-07-07', end: '2020-07-10'},
{begin: "2020-07-13", end: "2020-07-15"}, {begin: '2020-07-13', end: '2020-07-15'},
]; ];
result = [ result = ['2020-07-11', '2020-07-12'];
"2020-07-11", expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(
"2020-07-12", result,
]; );
expect(EquipmentBooking.getValidRange(start, end, testDevice)).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 = ["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(
EquipmentBooking.generateMarkedDates(true, theme, range),
).toStrictEqual(result);
result = { result = {
"2020-07-11": { '2020-07-11': {
startingDay: true, startingDay: true,
endingDay: false, endingDay: false,
color: theme.colors.textDisabled color: theme.colors.textDisabled,
}, },
"2020-07-12": { '2020-07-12': {
startingDay: false, startingDay: false,
endingDay: false, endingDay: false,
color: theme.colors.textDisabled color: theme.colors.textDisabled,
}, },
"2020-07-13": { '2020-07-13': {
startingDay: false, startingDay: false,
endingDay: true, endingDay: true,
color: theme.colors.textDisabled color: theme.colors.textDisabled,
}, },
}; };
expect(EquipmentBooking.generateMarkedDates(false, theme, range)).toStrictEqual(result); expect(
EquipmentBooking.generateMarkedDates(false, theme, range),
).toStrictEqual(result);
result = { result = {
"2020-07-11": { '2020-07-11': {
startingDay: true, startingDay: true,
endingDay: false, endingDay: false,
color: theme.colors.textDisabled color: theme.colors.textDisabled,
}, },
"2020-07-12": { '2020-07-12': {
startingDay: false, startingDay: false,
endingDay: false, endingDay: false,
color: theme.colors.textDisabled color: theme.colors.textDisabled,
}, },
"2020-07-13": { '2020-07-13': {
startingDay: false, startingDay: false,
endingDay: true, endingDay: true,
color: theme.colors.textDisabled color: theme.colors.textDisabled,
}, },
}; };
range = EquipmentBooking.getValidRange(end, start, testDevice); range = EquipmentBooking.getValidRange(end, start, testDevice);
expect(EquipmentBooking.generateMarkedDates(false, theme, range)).toStrictEqual(result); 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,35 +1,41 @@
/* 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(
Planning.isEventDateStringFormatValid('3214-64-12 1:16:65'),
).toBeFalse();
expect(Planning.isEventDateStringFormatValid('garbage')).toBeFalse();
expect(Planning.isEventDateStringFormatValid('')).toBeFalse();
expect(Planning.isEventDateStringFormatValid(undefined)).toBeFalse(); expect(Planning.isEventDateStringFormatValid(undefined)).toBeFalse();
expect(Planning.isEventDateStringFormatValid(null)).toBeFalse(); expect(Planning.isEventDateStringFormatValid(null)).toBeFalse();
}); });
@ -37,136 +43,144 @@ test('isEventDateStringFormatValid', () => {
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);
@ -176,31 +190,29 @@ test('pushEventInOrder', () => {
}); });
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);

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(expectDate.getTime()); expect(getMachineEndDate({endTime: '23:10'}).getTime()).toBe(
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: "13:10"})).toBeNull();
jest.spyOn(Date, 'now')
.mockImplementation(() =>
new Date('2020-01-14T23:00:00.000Z').getTime()
); );
expect(getMachineEndDate({endTime: '15:30'})).toBeNull();
expect(getMachineEndDate({endTime: '13:10'})).toBeNull();
jest
.spyOn(Date, 'now')
.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(expectDate.getTime()); expect(getMachineEndDate({endTime: '00:30'}).getTime()).toBe(
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,19 +1,20 @@
/* 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([
@ -89,9 +90,11 @@ test('clearLines', () => {
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
.spyOn(GridManager.prototype, 'getLinesToClear')
.mockImplementation(() => {}); .mockImplementation(() => {});
let spy2 = jest.spyOn(GridManager.prototype, 'clearLines') let spy2 = jest
.spyOn(GridManager.prototype, 'clearLines')
.mockImplementation(() => {}); .mockImplementation(() => {});
g.freezeTetromino(new Piece({}), null); g.freezeTetromino(new Piece({}), null);

View file

@ -1,17 +1,22 @@
/* 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(() => {
@ -21,8 +26,11 @@ afterAll(() => {
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')
.mockImplementation(() => {
return [{x: x, y: y}];
});
let grid = [ let grid = [
[{isEmpty: true}, {isEmpty: true}], [{isEmpty: true}, {isEmpty: true}],
[{isEmpty: true}, {isEmpty: false}], [{isEmpty: true}, {isEmpty: false}],
@ -31,19 +39,26 @@ test('isPositionValid', () => {
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;
y = 0;
expect(p.isPositionValid(grid, size, size)).toBeTrue(); expect(p.isPositionValid(grid, size, size)).toBeTrue();
x = 0; y = 1; x = 0;
y = 1;
expect(p.isPositionValid(grid, size, size)).toBeTrue(); expect(p.isPositionValid(grid, size, size)).toBeTrue();
x = 1; y = 1; x = 1;
y = 1;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); expect(p.isPositionValid(grid, size, size)).toBeFalse();
x = 2; y = 0; x = 2;
y = 0;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); expect(p.isPositionValid(grid, size, size)).toBeFalse();
x = -1; y = 0; x = -1;
y = 0;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); expect(p.isPositionValid(grid, size, size)).toBeFalse();
x = 0; y = 2; x = 0;
y = 2;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); expect(p.isPositionValid(grid, size, size)).toBeFalse();
x = 0; y = -1; x = 0;
y = -1;
expect(p.isPositionValid(grid, size, size)).toBeFalse(); expect(p.isPositionValid(grid, size, size)).toBeFalse();
spy.mockRestore(); spy.mockRestore();
@ -53,12 +68,15 @@ 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') });
let spy2 = jest
.spyOn(Piece.prototype, 'removeFromGrid')
.mockImplementation(() => {}); .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;
@ -82,12 +100,15 @@ test('tryMove', () => {
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') });
let spy2 = jest
.spyOn(Piece.prototype, 'removeFromGrid')
.mockImplementation(() => {}); .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;
@ -101,14 +122,17 @@ test('tryRotate', () => {
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 spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
return colors.tetrisI;
});
let grid = [ let grid = [
[{isEmpty: true}, {isEmpty: true}], [{isEmpty: true}, {isEmpty: true}],
[{isEmpty: true}, {isEmpty: true}], [{isEmpty: true}, {isEmpty: true}],
@ -141,11 +165,18 @@ test('removeFromGrid', () => {
{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
.spyOn(ShapeI.prototype, 'getCellsCoordinates')
.mockImplementation(() => {
return oldCoord;
});
let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => {
return colors.tetrisI;
});
let p = new Piece(colors); let p = new Piece(colors);
p.removeFromGrid(gridOld); p.removeFromGrid(gridOld);
expect(gridOld).toStrictEqual(gridNew); expect(gridOld).toStrictEqual(gridNew);

View file

@ -1,6 +1,7 @@
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();

View file

@ -1,6 +1,8 @@
/* 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',
@ -22,7 +24,7 @@ test('constructor', () => {
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);