diff --git a/__tests__/managers/ConnectionManager.test.js b/__tests__/managers/ConnectionManager.test.js index e307fd5..7e5cc6b 100644 --- a/__tests__/managers/ConnectionManager.test.js +++ b/__tests__/managers/ConnectionManager.test.js @@ -1,210 +1,217 @@ -jest.mock('react-native-keychain'); +/* eslint-disable */ import React from 'react'; -import ConnectionManager from "../../src/managers/ConnectionManager"; -import {ERROR_TYPE} from "../../src/utils/WebData"; +import ConnectionManager from '../../src/managers/ConnectionManager'; +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(); afterEach(() => { - jest.restoreAllMocks(); + jest.restoreAllMocks(); }); test('isLoggedIn yes', () => { - jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { - return 'token'; + jest + .spyOn(ConnectionManager.prototype, 'getToken') + .mockImplementationOnce(() => { + return 'token'; }); - return expect(c.isLoggedIn()).toBe(true); + return expect(c.isLoggedIn()).toBe(true); }); test('isLoggedIn no', () => { - jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { - return null; + jest + .spyOn(ConnectionManager.prototype, 'getToken') + .mockImplementationOnce(() => { + return null; }); - return expect(c.isLoggedIn()).toBe(false); + return expect(c.isLoggedIn()).toBe(false); }); -test("isConnectionResponseValid", () => { - 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(() => { + return Promise.resolve({ + json: () => { + return { + error: ERROR_TYPE.BAD_CREDENTIALS, + data: {}, + }; + }, + }); + }); + return expect(c.connect('email', 'password')).rejects.toBe( + ERROR_TYPE.BAD_CREDENTIALS, + ); }); -test("connect bad credentials", () => { - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.resolve({ - json: () => { - return { - error: ERROR_TYPE.BAD_CREDENTIALS, - data: {} - }; - }, - }) +test('connect good credentials', () => { + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + return Promise.resolve({ + json: () => { + return { + error: ERROR_TYPE.SUCCESS, + 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", () => { - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.resolve({ - json: () => { - return { - error: ERROR_TYPE.SUCCESS, - data: {token: 'token'} - }; - }, - }) +test('connect good credentials no consent', () => { + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + return Promise.resolve({ + json: () => { + return { + error: ERROR_TYPE.NO_CONSENT, + data: {}, + }; + }, }); - jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => { - return Promise.resolve(true); - }); - return expect(c.connect('email', 'password')).resolves.toBeTruthy(); + }); + return expect(c.connect('email', 'password')).rejects.toBe( + ERROR_TYPE.NO_CONSENT, + ); }); -test("connect good credentials no consent", () => { - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.resolve({ - json: () => { - return { - error: ERROR_TYPE.NO_CONSENT, - data: {} - }; - }, - }) +test('connect good credentials, fail save token', () => { + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + return Promise.resolve({ + json: () => { + return { + error: ERROR_TYPE.SUCCESS, + 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", () => { - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.resolve({ - json: () => { - return { - error: ERROR_TYPE.SUCCESS, - 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', () => { + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + return Promise.reject(); + }); + return expect(c.connect('email', 'password')).rejects.toBe( + ERROR_TYPE.CONNECTION_ERROR, + ); }); -test("connect connection error", () => { - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.reject(); +test('connect bogus response 1', () => { + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + 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", () => { - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.resolve({ - json: () => { - return { - thing: true, - wrong: '', - } - }, - }) +test('authenticatedRequest success', () => { + jest + .spyOn(ConnectionManager.prototype, 'getToken') + .mockImplementationOnce(() => { + return 'token'; }); - return expect(c.connect('email', 'password')) - .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + 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 success", () => { - jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { - return 'token'; +test('authenticatedRequest error wrong token', () => { + jest + .spyOn(ConnectionManager.prototype, 'getToken') + .mockImplementationOnce(() => { + return 'token'; }); - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.resolve({ - json: () => { - return { - error: ERROR_TYPE.SUCCESS, - data: {coucou: 'toi'} - }; - }, - }) + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + return Promise.resolve({ + json: () => { + return { + error: ERROR_TYPE.BAD_TOKEN, + 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", () => { - jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { - return 'token'; +test('authenticatedRequest error bogus response', () => { + jest + .spyOn(ConnectionManager.prototype, 'getToken') + .mockImplementationOnce(() => { + return 'token'; }); - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.resolve({ - json: () => { - return { - error: ERROR_TYPE.BAD_TOKEN, - data: {} - }; - }, - }) + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + return Promise.resolve({ + json: () => { + return { + error: ERROR_TYPE.SUCCESS, + }; + }, }); - 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", () => { - jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { - return 'token'; +test('authenticatedRequest connection error', () => { + jest + .spyOn(ConnectionManager.prototype, 'getToken') + .mockImplementationOnce(() => { + return 'token'; }); - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.resolve({ - json: () => { - return { - error: ERROR_TYPE.SUCCESS, - }; - }, - }) - }); - return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check')) - .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + return Promise.reject(); + }); + return expect( + c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'), + ).rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); }); -test("authenticatedRequest connection error", () => { - jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { - return 'token'; +test('authenticatedRequest error no token', () => { + jest + .spyOn(ConnectionManager.prototype, 'getToken') + .mockImplementationOnce(() => { + return null; }); - jest.spyOn(global, 'fetch').mockImplementationOnce(() => { - return Promise.reject() - }); - 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); + return expect( + c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'), + ).rejects.toBe(ERROR_TYPE.UNKNOWN); }); diff --git a/__tests__/utils/EquipmentBooking.test.js b/__tests__/utils/EquipmentBooking.test.js index f20a169..1405ed3 100644 --- a/__tests__/utils/EquipmentBooking.test.js +++ b/__tests__/utils/EquipmentBooking.test.js @@ -1,319 +1,345 @@ +/* eslint-disable */ + import React from 'react'; -import * as EquipmentBooking from "../../src/utils/EquipmentBooking"; -import i18n from "i18n-js"; +import * as EquipmentBooking from '../../src/utils/EquipmentBooking'; +import i18n from 'i18n-js'; test('getISODate', () => { - let date = new Date("2020-03-05 12:00"); - expect(EquipmentBooking.getISODate(date)).toBe("2020-03-05"); - date = new Date("2020-03-05"); - expect(EquipmentBooking.getISODate(date)).toBe("2020-03-05"); - date = new Date("2020-03-05 00:00"); // Treated as local time - expect(EquipmentBooking.getISODate(date)).toBe("2020-03-04"); // Treated as UTC + let date = new Date('2020-03-05 12:00'); + expect(EquipmentBooking.getISODate(date)).toBe('2020-03-05'); + date = new Date('2020-03-05'); + expect(EquipmentBooking.getISODate(date)).toBe('2020-03-05'); + date = new Date('2020-03-05 00:00'); // Treated as local time + expect(EquipmentBooking.getISODate(date)).toBe('2020-03-04'); // Treated as UTC }); test('getCurrentDay', () => { - jest.spyOn(Date, 'now') - .mockImplementation(() => - new Date('2020-01-14 14:50:35').getTime() - ); - expect(EquipmentBooking.getCurrentDay().getTime()).toBe(new Date("2020-01-14").getTime()); + jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date('2020-01-14 14:50:35').getTime()); + expect(EquipmentBooking.getCurrentDay().getTime()).toBe( + new Date('2020-01-14').getTime(), + ); }); test('isEquipmentAvailable', () => { - jest.spyOn(Date, 'now') - .mockImplementation(() => - new Date('2020-07-09').getTime() - ); - let testDevice = { - id: 1, - name: "Petit barbecue", - caution: 100, - booked_at: [{begin: "2020-07-07", end: "2020-07-10"}] - }; - expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse(); + jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date('2020-07-09').getTime()); + let testDevice = { + id: 1, + name: 'Petit barbecue', + caution: 100, + booked_at: [{begin: '2020-07-07', end: '2020-07-10'}], + }; + expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse(); - testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-09"}]; - expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse(); + testDevice.booked_at = [{begin: '2020-07-07', end: '2020-07-09'}]; + expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse(); - testDevice.booked_at = [{begin: "2020-07-09", end: "2020-07-10"}]; - expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse(); + testDevice.booked_at = [{begin: '2020-07-09', end: '2020-07-10'}]; + expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeFalse(); - testDevice.booked_at = [ - {begin: "2020-07-07", end: "2020-07-8"}, - {begin: "2020-07-10", end: "2020-07-12"}, - ]; - expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeTrue(); + testDevice.booked_at = [ + {begin: '2020-07-07', end: '2020-07-8'}, + {begin: '2020-07-10', end: '2020-07-12'}, + ]; + expect(EquipmentBooking.isEquipmentAvailable(testDevice)).toBeTrue(); }); test('getFirstEquipmentAvailability', () => { - jest.spyOn(Date, 'now') - .mockImplementation(() => - new Date('2020-07-09').getTime() - ); - let testDevice = { - id: 1, - name: "Petit barbecue", - caution: 100, - booked_at: [{begin: "2020-07-07", end: "2020-07-10"}] - }; - expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).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 = [ - {begin: "2020-07-07", end: "2020-07-09"}, - {begin: "2020-07-10", end: "2020-07-16"}, - ]; - expect(EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime()).toBe(new Date("2020-07-17").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()); + jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date('2020-07-09').getTime()); + let testDevice = { + id: 1, + name: 'Petit barbecue', + caution: 100, + booked_at: [{begin: '2020-07-07', end: '2020-07-10'}], + }; + expect( + EquipmentBooking.getFirstEquipmentAvailability(testDevice).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 = [ + {begin: '2020-07-07', end: '2020-07-09'}, + {begin: '2020-07-10', end: '2020-07-16'}, + ]; + expect( + EquipmentBooking.getFirstEquipmentAvailability(testDevice).getTime(), + ).toBe(new Date('2020-07-17').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', () => { - jest.spyOn(Date, 'now') - .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"); - 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"); + jest + .spyOn(Date, 'now') + .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', + ); + 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', () => { - let testDevice = { - id: 1, - name: "Petit barbecue", - caution: 100, - booked_at: [{begin: "2020-07-07", end: "2020-07-10"}] - }; - let start = new Date("2020-07-11"); - let end = new Date("2020-07-15"); - let result = [ - "2020-07-11", - "2020-07-12", - "2020-07-13", - "2020-07-14", - "2020-07-15", - ]; - expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result); - testDevice.booked_at = [ - {begin: "2020-07-07", end: "2020-07-10"}, - {begin: "2020-07-13", end: "2020-07-15"}, - ]; - result = [ - "2020-07-11", - "2020-07-12", - ]; - expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result); + let testDevice = { + id: 1, + name: 'Petit barbecue', + caution: 100, + booked_at: [{begin: '2020-07-07', end: '2020-07-10'}], + }; + let start = new Date('2020-07-11'); + let end = new Date('2020-07-15'); + let result = [ + '2020-07-11', + '2020-07-12', + '2020-07-13', + '2020-07-14', + '2020-07-15', + ]; + expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual( + result, + ); + testDevice.booked_at = [ + {begin: '2020-07-07', end: '2020-07-10'}, + {begin: '2020-07-13', end: '2020-07-15'}, + ]; + result = ['2020-07-11', '2020-07-12']; + expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual( + result, + ); - testDevice.booked_at = [{begin: "2020-07-12", end: "2020-07-13"}]; - result = ["2020-07-11"]; - expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result); - testDevice.booked_at = [{begin: "2020-07-07", end: "2020-07-12"},]; - result = [ - "2020-07-13", - "2020-07-14", - "2020-07-15", - ]; - expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result); - start = new Date("2020-07-14"); - end = new Date("2020-07-14"); - result = [ - "2020-07-14", - ]; - expect(EquipmentBooking.getValidRange(start, start, testDevice)).toStrictEqual(result); - expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result); - expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual(result); + testDevice.booked_at = [{begin: '2020-07-12', end: '2020-07-13'}]; + result = ['2020-07-11']; + expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual( + result, + ); + testDevice.booked_at = [{begin: '2020-07-07', end: '2020-07-12'}]; + result = ['2020-07-13', '2020-07-14', '2020-07-15']; + expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual( + result, + ); + start = new Date('2020-07-14'); + end = new Date('2020-07-14'); + result = ['2020-07-14']; + expect( + EquipmentBooking.getValidRange(start, start, testDevice), + ).toStrictEqual(result); + expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual( + result, + ); + expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual( + result, + ); - start = new Date("2020-07-14"); - end = new Date("2020-07-17"); - result = [ - "2020-07-14", - "2020-07-15", - "2020-07-16", - "2020-07-17", - ]; - expect(EquipmentBooking.getValidRange(start, end, null)).toStrictEqual(result); + start = new Date('2020-07-14'); + end = new Date('2020-07-17'); + result = ['2020-07-14', '2020-07-15', '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"}]; - result = [ - "2020-07-14", - "2020-07-15", - "2020-07-16", - ]; - expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual(result); + testDevice.booked_at = [{begin: '2020-07-17', end: '2020-07-17'}]; + result = ['2020-07-14', '2020-07-15', '2020-07-16']; + expect(EquipmentBooking.getValidRange(start, end, testDevice)).toStrictEqual( + result, + ); - testDevice.booked_at = [ - {begin: "2020-07-12", end: "2020-07-13"}, - {begin: "2020-07-15", end: "2020-07-20"}, - ]; - start = new Date("2020-07-11"); - end = new Date("2020-07-23"); - result = [ - "2020-07-21", - "2020-07-22", - "2020-07-23", - ]; - expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual(result); + testDevice.booked_at = [ + {begin: '2020-07-12', end: '2020-07-13'}, + {begin: '2020-07-15', end: '2020-07-20'}, + ]; + start = new Date('2020-07-11'); + end = new Date('2020-07-23'); + result = ['2020-07-21', '2020-07-22', '2020-07-23']; + expect(EquipmentBooking.getValidRange(end, start, testDevice)).toStrictEqual( + result, + ); }); test('generateMarkedDates', () => { - let theme = { - colors: { - primary: "primary", - danger: "primary", - textDisabled: "primary", - } - } - let testDevice = { - id: 1, - name: "Petit barbecue", - caution: 100, - booked_at: [{begin: "2020-07-07", end: "2020-07-10"}] - }; - let start = new Date("2020-07-11"); - let end = new Date("2020-07-13"); - let range = EquipmentBooking.getValidRange(start, end, testDevice); - let result = { - "2020-07-11": { - startingDay: true, - endingDay: false, - color: theme.colors.primary - }, - "2020-07-12": { - startingDay: false, - endingDay: false, - color: theme.colors.danger - }, - "2020-07-13": { - startingDay: false, - endingDay: true, - color: theme.colors.primary - }, - }; - expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); - result = { - "2020-07-11": { - startingDay: true, - endingDay: false, - color: theme.colors.textDisabled - }, - "2020-07-12": { - startingDay: false, - endingDay: false, - color: theme.colors.textDisabled - }, - "2020-07-13": { - startingDay: false, - endingDay: true, - color: theme.colors.textDisabled - }, - }; - expect(EquipmentBooking.generateMarkedDates(false, theme, range)).toStrictEqual(result); - result = { - "2020-07-11": { - startingDay: true, - endingDay: false, - color: theme.colors.textDisabled - }, - "2020-07-12": { - startingDay: false, - endingDay: false, - color: theme.colors.textDisabled - }, - "2020-07-13": { - startingDay: false, - endingDay: true, - color: theme.colors.textDisabled - }, - }; - range = EquipmentBooking.getValidRange(end, start, testDevice); - expect(EquipmentBooking.generateMarkedDates(false, theme, range)).toStrictEqual(result); + let theme = { + colors: { + primary: 'primary', + danger: 'primary', + textDisabled: 'primary', + }, + }; + let testDevice = { + id: 1, + name: 'Petit barbecue', + caution: 100, + booked_at: [{begin: '2020-07-07', end: '2020-07-10'}], + }; + let start = new Date('2020-07-11'); + let end = new Date('2020-07-13'); + let range = EquipmentBooking.getValidRange(start, end, testDevice); + let result = { + '2020-07-11': { + startingDay: true, + endingDay: false, + color: theme.colors.primary, + }, + '2020-07-12': { + startingDay: false, + endingDay: false, + color: theme.colors.danger, + }, + '2020-07-13': { + startingDay: false, + endingDay: true, + color: theme.colors.primary, + }, + }; + expect( + EquipmentBooking.generateMarkedDates(true, theme, range), + ).toStrictEqual(result); + result = { + '2020-07-11': { + startingDay: true, + endingDay: false, + color: theme.colors.textDisabled, + }, + '2020-07-12': { + startingDay: false, + endingDay: false, + color: theme.colors.textDisabled, + }, + '2020-07-13': { + startingDay: false, + endingDay: true, + color: theme.colors.textDisabled, + }, + }; + expect( + EquipmentBooking.generateMarkedDates(false, theme, range), + ).toStrictEqual(result); + result = { + '2020-07-11': { + startingDay: true, + endingDay: false, + color: theme.colors.textDisabled, + }, + '2020-07-12': { + startingDay: false, + endingDay: false, + color: theme.colors.textDisabled, + }, + '2020-07-13': { + startingDay: false, + endingDay: true, + 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"},]; - result = { - "2020-07-11": { - startingDay: true, - endingDay: false, - color: theme.colors.primary - }, - "2020-07-12": { - startingDay: false, - endingDay: true, - color: theme.colors.primary - }, - }; - range = EquipmentBooking.getValidRange(start, end, testDevice); - expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); + testDevice.booked_at = [{begin: '2020-07-13', end: '2020-07-15'}]; + result = { + '2020-07-11': { + startingDay: true, + endingDay: false, + color: theme.colors.primary, + }, + '2020-07-12': { + startingDay: false, + endingDay: true, + color: theme.colors.primary, + }, + }; + range = EquipmentBooking.getValidRange(start, end, testDevice); + expect( + EquipmentBooking.generateMarkedDates(true, theme, range), + ).toStrictEqual(result); - testDevice.booked_at = [{begin: "2020-07-12", end: "2020-07-13"},]; - result = { - "2020-07-11": { - startingDay: true, - endingDay: true, - color: theme.colors.primary - }, - }; - range = EquipmentBooking.getValidRange(start, end, testDevice); - expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); + testDevice.booked_at = [{begin: '2020-07-12', end: '2020-07-13'}]; + result = { + '2020-07-11': { + startingDay: true, + endingDay: true, + color: theme.colors.primary, + }, + }; + range = EquipmentBooking.getValidRange(start, end, testDevice); + expect( + EquipmentBooking.generateMarkedDates(true, theme, range), + ).toStrictEqual(result); - testDevice.booked_at = [ - {begin: "2020-07-12", end: "2020-07-13"}, - {begin: "2020-07-15", end: "2020-07-20"}, - ]; - start = new Date("2020-07-11"); - end = new Date("2020-07-23"); - result = { - "2020-07-11": { - startingDay: true, - endingDay: true, - color: theme.colors.primary - }, - }; - range = EquipmentBooking.getValidRange(start, end, testDevice); - expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); + testDevice.booked_at = [ + {begin: '2020-07-12', end: '2020-07-13'}, + {begin: '2020-07-15', end: '2020-07-20'}, + ]; + start = new Date('2020-07-11'); + end = new Date('2020-07-23'); + result = { + '2020-07-11': { + startingDay: true, + endingDay: true, + color: theme.colors.primary, + }, + }; + range = EquipmentBooking.getValidRange(start, end, testDevice); + expect( + EquipmentBooking.generateMarkedDates(true, theme, range), + ).toStrictEqual(result); - result = { - "2020-07-21": { - startingDay: true, - endingDay: false, - color: theme.colors.primary - }, - "2020-07-22": { - startingDay: false, - endingDay: false, - color: theme.colors.danger - }, - "2020-07-23": { - startingDay: false, - endingDay: true, - color: theme.colors.primary - }, - }; - range = EquipmentBooking.getValidRange(end, start, testDevice); - expect(EquipmentBooking.generateMarkedDates(true, theme, range)).toStrictEqual(result); + result = { + '2020-07-21': { + startingDay: true, + endingDay: false, + color: theme.colors.primary, + }, + '2020-07-22': { + startingDay: false, + endingDay: false, + color: theme.colors.danger, + }, + '2020-07-23': { + startingDay: false, + endingDay: true, + color: theme.colors.primary, + }, + }; + range = EquipmentBooking.getValidRange(end, start, testDevice); + expect( + EquipmentBooking.generateMarkedDates(true, theme, range), + ).toStrictEqual(result); }); diff --git a/__tests__/utils/PlanningEventManager.test.js b/__tests__/utils/PlanningEventManager.test.js index 21d972a..ad57c6e 100644 --- a/__tests__/utils/PlanningEventManager.test.js +++ b/__tests__/utils/PlanningEventManager.test.js @@ -1,210 +1,222 @@ +/* eslint-disable */ + import React from 'react'; -import * as Planning from "../../src/utils/Planning"; +import * as Planning from '../../src/utils/Planning'; test('isDescriptionEmpty', () => { - expect(Planning.isDescriptionEmpty("")).toBeTrue(); - expect(Planning.isDescriptionEmpty(" ")).toBeTrue(); - // noinspection CheckTagEmptyBody - expect(Planning.isDescriptionEmpty("

")).toBeTrue(); - expect(Planning.isDescriptionEmpty("

")).toBeTrue(); - expect(Planning.isDescriptionEmpty("


")).toBeTrue(); - expect(Planning.isDescriptionEmpty("



")).toBeTrue(); - expect(Planning.isDescriptionEmpty("




")).toBeTrue(); - expect(Planning.isDescriptionEmpty("


")).toBeTrue(); - expect(Planning.isDescriptionEmpty(null)).toBeTrue(); - expect(Planning.isDescriptionEmpty(undefined)).toBeTrue(); - expect(Planning.isDescriptionEmpty("coucou")).toBeFalse(); - expect(Planning.isDescriptionEmpty("

coucou

")).toBeFalse(); + expect(Planning.isDescriptionEmpty('')).toBeTrue(); + expect(Planning.isDescriptionEmpty(' ')).toBeTrue(); + // noinspection CheckTagEmptyBody + expect(Planning.isDescriptionEmpty('

')).toBeTrue(); + expect(Planning.isDescriptionEmpty('

')).toBeTrue(); + expect(Planning.isDescriptionEmpty('


')).toBeTrue(); + expect(Planning.isDescriptionEmpty('



')).toBeTrue(); + expect(Planning.isDescriptionEmpty('




')).toBeTrue(); + expect(Planning.isDescriptionEmpty('


')).toBeTrue(); + expect(Planning.isDescriptionEmpty(null)).toBeTrue(); + expect(Planning.isDescriptionEmpty(undefined)).toBeTrue(); + expect(Planning.isDescriptionEmpty('coucou')).toBeFalse(); + expect(Planning.isDescriptionEmpty('

coucou

')).toBeFalse(); }); test('isEventDateStringFormatValid', () => { - expect(Planning.isEventDateStringFormatValid("2020-03-21 09:00")).toBeTrue(); - expect(Planning.isEventDateStringFormatValid("3214-64-12 01:16")).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:00")).toBeFalse(); - expect(Planning.isEventDateStringFormatValid("3214-64-12 1:16")).toBeFalse(); - expect(Planning.isEventDateStringFormatValid("3214-f4-12 01:16")).toBeFalse(); - expect(Planning.isEventDateStringFormatValid("sqdd 09:00")).toBeFalse(); - expect(Planning.isEventDateStringFormatValid("2020-03-21")).toBeFalse(); - expect(Planning.isEventDateStringFormatValid("2020-03-21 truc")).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(null)).toBeFalse(); + expect( + Planning.isEventDateStringFormatValid('3214-64-12 01:16:00'), + ).toBeFalse(); + expect(Planning.isEventDateStringFormatValid('3214-64-12 1:16')).toBeFalse(); + expect(Planning.isEventDateStringFormatValid('3214-f4-12 01:16')).toBeFalse(); + expect(Planning.isEventDateStringFormatValid('sqdd 09:00')).toBeFalse(); + expect(Planning.isEventDateStringFormatValid('2020-03-21')).toBeFalse(); + expect(Planning.isEventDateStringFormatValid('2020-03-21 truc')).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(null)).toBeFalse(); }); test('stringToDate', () => { - let testDate = new Date(); - expect(Planning.stringToDate(undefined)).toBeNull(); - expect(Planning.stringToDate("")).toBeNull(); - expect(Planning.stringToDate("garbage")).toBeNull(); - expect(Planning.stringToDate("2020-03-21")).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(); - testDate.setFullYear(2020, 2, 21); - testDate.setHours(9, 0, 0, 0); - expect(Planning.stringToDate("2020-03-21 09:00")).toEqual(testDate); - testDate.setFullYear(2020, 0, 31); - testDate.setHours(18, 30, 0, 0); - expect(Planning.stringToDate("2020-01-31 18:30")).toEqual(testDate); - testDate.setFullYear(2020, 50, 50); - testDate.setHours(65, 65, 0, 0); - expect(Planning.stringToDate("2020-51-50 65:65")).toEqual(testDate); + let testDate = new Date(); + expect(Planning.stringToDate(undefined)).toBeNull(); + expect(Planning.stringToDate('')).toBeNull(); + expect(Planning.stringToDate('garbage')).toBeNull(); + expect(Planning.stringToDate('2020-03-21')).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(); + testDate.setFullYear(2020, 2, 21); + testDate.setHours(9, 0, 0, 0); + expect(Planning.stringToDate('2020-03-21 09:00')).toEqual(testDate); + testDate.setFullYear(2020, 0, 31); + testDate.setHours(18, 30, 0, 0); + expect(Planning.stringToDate('2020-01-31 18:30')).toEqual(testDate); + testDate.setFullYear(2020, 50, 50); + testDate.setHours(65, 65, 0, 0); + expect(Planning.stringToDate('2020-51-50 65:65')).toEqual(testDate); }); test('getFormattedEventTime', () => { - expect(Planning.getFormattedEventTime(null, null)) - .toBe('/ - /'); - expect(Planning.getFormattedEventTime(undefined, undefined)) - .toBe('/ - /'); - expect(Planning.getFormattedEventTime("20:30", "23:00")) - .toBe('/ - /'); - expect(Planning.getFormattedEventTime("2020-03-30", "2020-03-31")) - .toBe('/ - /'); + expect(Planning.getFormattedEventTime(null, null)).toBe('/ - /'); + expect(Planning.getFormattedEventTime(undefined, undefined)).toBe('/ - /'); + expect(Planning.getFormattedEventTime('20:30', '23:00')).toBe('/ - /'); + expect(Planning.getFormattedEventTime('2020-03-30', '2020-03-31')).toBe( + '/ - /', + ); - - expect(Planning.getFormattedEventTime("2020-03-21 09:00", "2020-03-21 09:00")) - .toBe('09:00'); - expect(Planning.getFormattedEventTime("2020-03-21 09:00", "2020-03-22 17:00")) - .toBe('09:00 - 23:59'); - expect(Planning.getFormattedEventTime("2020-03-30 20:30", "2020-03-30 23:00")) - .toBe('20:30 - 23:00'); + expect( + Planning.getFormattedEventTime('2020-03-21 09:00', '2020-03-21 09:00'), + ).toBe('09:00'); + expect( + Planning.getFormattedEventTime('2020-03-21 09:00', '2020-03-22 17:00'), + ).toBe('09:00 - 23:59'); + expect( + Planning.getFormattedEventTime('2020-03-30 20:30', '2020-03-30 23:00'), + ).toBe('20:30 - 23:00'); }); test('getDateOnlyString', () => { - 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-o5 09:00")).toBeNull(); - expect(Planning.getDateOnlyString("2021-12-15 09:")).toBeNull(); - expect(Planning.getDateOnlyString("2021-12-15")).toBeNull(); - expect(Planning.getDateOnlyString("garbage")).toBeNull(); + 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-o5 09:00')).toBeNull(); + expect(Planning.getDateOnlyString('2021-12-15 09:')).toBeNull(); + expect(Planning.getDateOnlyString('2021-12-15')).toBeNull(); + expect(Planning.getDateOnlyString('garbage')).toBeNull(); }); test('isEventBefore', () => { - expect(Planning.isEventBefore( - "2020-03-21 09:00", "2020-03-21 10:00")).toBeTrue(); - expect(Planning.isEventBefore( - "2020-03-21 10:00", "2020-03-21 10:15")).toBeTrue(); - expect(Planning.isEventBefore( - "2020-03-21 10:15", "2021-03-21 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('2020-03-21 09:00', '2020-03-21 10:00'), + ).toBeTrue(); + expect( + Planning.isEventBefore('2020-03-21 10:00', '2020-03-21 10:15'), + ).toBeTrue(); + expect( + Planning.isEventBefore('2020-03-21 10:15', '2021-03-21 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( - "2020-03-21 10:00", "2020-03-21 10:00")).toBeFalse(); - expect(Planning.isEventBefore( - "2020-03-21 10:00", "2020-03-21 09:00")).toBeFalse(); - expect(Planning.isEventBefore( - "2020-03-21 10:15", "2020-03-21 10:00")).toBeFalse(); - expect(Planning.isEventBefore( - "2021-03-21 10:15", "2020-03-21 10:15")).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('2020-03-21 10:00', '2020-03-21 10:00'), + ).toBeFalse(); + expect( + Planning.isEventBefore('2020-03-21 10:00', '2020-03-21 09:00'), + ).toBeFalse(); + expect( + Planning.isEventBefore('2020-03-21 10:15', '2020-03-21 10:00'), + ).toBeFalse(); + expect( + Planning.isEventBefore('2021-03-21 10:15', '2020-03-21 10:15'), + ).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( - "garbage", "2020-03-21 10:15")).toBeFalse(); - expect(Planning.isEventBefore( - undefined, undefined)).toBeFalse(); + expect(Planning.isEventBefore('garbage', '2020-03-21 10:15')).toBeFalse(); + expect(Planning.isEventBefore(undefined, undefined)).toBeFalse(); }); test('dateToString', () => { - let testDate = new Date(); - testDate.setFullYear(2020, 2, 21); - testDate.setHours(9, 0, 0, 0); - expect(Planning.dateToString(testDate)).toBe("2020-03-21 09:00"); - testDate.setFullYear(2021, 0, 12); - testDate.setHours(9, 10, 0, 0); - expect(Planning.dateToString(testDate)).toBe("2021-01-12 09:10"); - testDate.setFullYear(2022, 11, 31); - testDate.setHours(9, 10, 15, 0); - expect(Planning.dateToString(testDate)).toBe("2022-12-31 09:10"); + let testDate = new Date(); + testDate.setFullYear(2020, 2, 21); + testDate.setHours(9, 0, 0, 0); + expect(Planning.dateToString(testDate)).toBe('2020-03-21 09:00'); + testDate.setFullYear(2021, 0, 12); + testDate.setHours(9, 10, 0, 0); + expect(Planning.dateToString(testDate)).toBe('2021-01-12 09:10'); + testDate.setFullYear(2022, 11, 31); + testDate.setHours(9, 10, 15, 0); + expect(Planning.dateToString(testDate)).toBe('2022-12-31 09:10'); }); test('generateEmptyCalendar', () => { - jest.spyOn(Date, 'now') - .mockImplementation(() => - new Date('2020-01-14T00:00:00.000Z').getTime() - ); - let calendar = Planning.generateEmptyCalendar(1); - expect(calendar).toHaveProperty("2020-01-14"); - expect(calendar).toHaveProperty("2020-01-20"); - expect(calendar).toHaveProperty("2020-02-10"); - expect(Object.keys(calendar).length).toBe(32); - calendar = Planning.generateEmptyCalendar(3); - expect(calendar).toHaveProperty("2020-01-14"); - expect(calendar).toHaveProperty("2020-01-20"); - expect(calendar).toHaveProperty("2020-02-10"); - expect(calendar).toHaveProperty("2020-02-14"); - expect(calendar).toHaveProperty("2020-03-20"); - expect(calendar).toHaveProperty("2020-04-12"); - expect(Object.keys(calendar).length).toBe(92); + jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date('2020-01-14T00:00:00.000Z').getTime()); + let calendar = Planning.generateEmptyCalendar(1); + expect(calendar).toHaveProperty('2020-01-14'); + expect(calendar).toHaveProperty('2020-01-20'); + expect(calendar).toHaveProperty('2020-02-10'); + expect(Object.keys(calendar).length).toBe(32); + calendar = Planning.generateEmptyCalendar(3); + expect(calendar).toHaveProperty('2020-01-14'); + expect(calendar).toHaveProperty('2020-01-20'); + expect(calendar).toHaveProperty('2020-02-10'); + expect(calendar).toHaveProperty('2020-02-14'); + expect(calendar).toHaveProperty('2020-03-20'); + expect(calendar).toHaveProperty('2020-04-12'); + expect(Object.keys(calendar).length).toBe(92); }); test('pushEventInOrder', () => { - let eventArray = []; - let event1 = {date_begin: "2020-01-14 09:15"}; - Planning.pushEventInOrder(eventArray, event1); - expect(eventArray.length).toBe(1); - expect(eventArray[0]).toBe(event1); + let eventArray = []; + let event1 = {date_begin: '2020-01-14 09:15'}; + Planning.pushEventInOrder(eventArray, event1); + expect(eventArray.length).toBe(1); + expect(eventArray[0]).toBe(event1); - let event2 = {date_begin: "2020-01-14 10:15"}; - Planning.pushEventInOrder(eventArray, event2); - expect(eventArray.length).toBe(2); - expect(eventArray[0]).toBe(event1); - expect(eventArray[1]).toBe(event2); + let event2 = {date_begin: '2020-01-14 10:15'}; + Planning.pushEventInOrder(eventArray, event2); + expect(eventArray.length).toBe(2); + expect(eventArray[0]).toBe(event1); + expect(eventArray[1]).toBe(event2); - let event3 = {date_begin: "2020-01-14 10:15", title: "garbage"}; - Planning.pushEventInOrder(eventArray, event3); - expect(eventArray.length).toBe(3); - expect(eventArray[0]).toBe(event1); - expect(eventArray[1]).toBe(event2); - expect(eventArray[2]).toBe(event3); + let event3 = {date_begin: '2020-01-14 10:15', title: 'garbage'}; + Planning.pushEventInOrder(eventArray, event3); + expect(eventArray.length).toBe(3); + expect(eventArray[0]).toBe(event1); + expect(eventArray[1]).toBe(event2); + expect(eventArray[2]).toBe(event3); - let event4 = {date_begin: "2020-01-13 09:00"}; - Planning.pushEventInOrder(eventArray, event4); - expect(eventArray.length).toBe(4); - expect(eventArray[0]).toBe(event4); - expect(eventArray[1]).toBe(event1); - expect(eventArray[2]).toBe(event2); - expect(eventArray[3]).toBe(event3); + let event4 = {date_begin: '2020-01-13 09:00'}; + Planning.pushEventInOrder(eventArray, event4); + expect(eventArray.length).toBe(4); + expect(eventArray[0]).toBe(event4); + expect(eventArray[1]).toBe(event1); + expect(eventArray[2]).toBe(event2); + expect(eventArray[3]).toBe(event3); }); test('generateEventAgenda', () => { - jest.spyOn(Date, 'now') - .mockImplementation(() => - new Date('2020-01-14T00:00:00.000Z').getTime() - ); - let eventList = [ - {date_begin: "2020-01-14 09:15"}, - {date_begin: "2020-02-01 09:15"}, - {date_begin: "2020-01-15 09:15"}, - {date_begin: "2020-02-01 09:30"}, - {date_begin: "2020-02-01 08:30"}, - ]; - const calendar = Planning.generateEventAgenda(eventList, 2); - expect(calendar["2020-01-14"].length).toBe(1); - expect(calendar["2020-01-14"][0]).toBe(eventList[0]); - expect(calendar["2020-01-15"].length).toBe(1); - expect(calendar["2020-01-15"][0]).toBe(eventList[2]); - expect(calendar["2020-02-01"].length).toBe(3); - expect(calendar["2020-02-01"][0]).toBe(eventList[4]); - expect(calendar["2020-02-01"][1]).toBe(eventList[1]); - expect(calendar["2020-02-01"][2]).toBe(eventList[3]); + jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date('2020-01-14T00:00:00.000Z').getTime()); + let eventList = [ + {date_begin: '2020-01-14 09:15'}, + {date_begin: '2020-02-01 09:15'}, + {date_begin: '2020-01-15 09:15'}, + {date_begin: '2020-02-01 09:30'}, + {date_begin: '2020-02-01 08:30'}, + ]; + const calendar = Planning.generateEventAgenda(eventList, 2); + expect(calendar['2020-01-14'].length).toBe(1); + expect(calendar['2020-01-14'][0]).toBe(eventList[0]); + expect(calendar['2020-01-15'].length).toBe(1); + expect(calendar['2020-01-15'][0]).toBe(eventList[2]); + expect(calendar['2020-02-01'].length).toBe(3); + expect(calendar['2020-02-01'][0]).toBe(eventList[4]); + expect(calendar['2020-02-01'][1]).toBe(eventList[1]); + expect(calendar['2020-02-01'][2]).toBe(eventList[3]); }); test('getCurrentDateString', () => { - jest.spyOn(Date, 'now') - .mockImplementation(() => { - let date = new Date(); - date.setFullYear(2020, 0, 14); - date.setHours(15, 30, 54, 65); - return date.getTime(); - }); - expect(Planning.getCurrentDateString()).toBe('2020-01-14 15:30'); + jest.spyOn(Date, 'now').mockImplementation(() => { + let date = new Date(); + date.setFullYear(2020, 0, 14); + date.setHours(15, 30, 54, 65); + return date.getTime(); + }); + expect(Planning.getCurrentDateString()).toBe('2020-01-14 15:30'); }); diff --git a/__tests__/utils/Proxiwash.test.js b/__tests__/utils/Proxiwash.test.js index 4aca8e7..4bb7c51 100644 --- a/__tests__/utils/Proxiwash.test.js +++ b/__tests__/utils/Proxiwash.test.js @@ -1,142 +1,167 @@ +/* eslint-disable */ + import React from 'react'; -import {getCleanedMachineWatched, getMachineEndDate, getMachineOfId, isMachineWatched} from "../../src/utils/Proxiwash"; +import { + getCleanedMachineWatched, + getMachineEndDate, + getMachineOfId, + isMachineWatched, +} from '../../src/utils/Proxiwash'; test('getMachineEndDate', () => { - jest.spyOn(Date, 'now') - .mockImplementation(() => - new Date('2020-01-14T15:00:00.000Z').getTime() - ); - let expectDate = new Date('2020-01-14T15:00:00.000Z'); - expectDate.setHours(23); - expectDate.setMinutes(10); - expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime()); + jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date('2020-01-14T15:00:00.000Z').getTime()); + let expectDate = new Date('2020-01-14T15:00:00.000Z'); + expectDate.setHours(23); + expectDate.setMinutes(10); + expect(getMachineEndDate({endTime: '23:10'}).getTime()).toBe( + expectDate.getTime(), + ); - expectDate.setHours(16); - expectDate.setMinutes(30); - expect(getMachineEndDate({endTime: "16:30"}).getTime()).toBe(expectDate.getTime()); + expectDate.setHours(16); + expectDate.setMinutes(30); + 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') - .mockImplementation(() => - new Date('2020-01-14T23:00:00.000Z').getTime() - ); - expectDate = new Date('2020-01-14T23:00:00.000Z'); - expectDate.setHours(0); - expectDate.setMinutes(30); - expect(getMachineEndDate({endTime: "00:30"}).getTime()).toBe(expectDate.getTime()); + jest + .spyOn(Date, 'now') + .mockImplementation(() => new Date('2020-01-14T23:00:00.000Z').getTime()); + expectDate = new Date('2020-01-14T23:00:00.000Z'); + expectDate.setHours(0); + expectDate.setMinutes(30); + expect(getMachineEndDate({endTime: '00:30'}).getTime()).toBe( + expectDate.getTime(), + ); }); test('isMachineWatched', () => { - let machineList = [ - { - number: "0", - endTime: "23:30", - }, - { - number: "1", - endTime: "20:30", - }, - ]; - expect(isMachineWatched({number: "0", endTime: "23:30"}, machineList)).toBeTrue(); - 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(); + let machineList = [ + { + number: '0', + endTime: '23:30', + }, + { + number: '1', + endTime: '20:30', + }, + ]; + expect( + isMachineWatched({number: '0', endTime: '23:30'}, machineList), + ).toBeTrue(); + 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', () => { - let machineList = [ - { - number: "0", - }, - { - number: "1", - }, - ]; - expect(getMachineOfId("0", machineList)).toStrictEqual({number: "0"}); - expect(getMachineOfId("1", machineList)).toStrictEqual({number: "1"}); - expect(getMachineOfId("3", machineList)).toBeNull(); + let machineList = [ + { + number: '0', + }, + { + number: '1', + }, + ]; + expect(getMachineOfId('0', machineList)).toStrictEqual({number: '0'}); + expect(getMachineOfId('1', machineList)).toStrictEqual({number: '1'}); + expect(getMachineOfId('3', machineList)).toBeNull(); }); test('getCleanedMachineWatched', () => { - let machineList = [ - { - number: "0", - endTime: "23:30", - }, - { - number: "1", - endTime: "20:30", - }, - { - number: "2", - endTime: "", - }, - ]; - let watchList = [ - { - number: "0", - endTime: "23:30", - }, - { - number: "1", - endTime: "20:30", - }, - { - number: "2", - endTime: "", - }, - ]; - let cleanedList = watchList; - expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); + let machineList = [ + { + number: '0', + endTime: '23:30', + }, + { + number: '1', + endTime: '20:30', + }, + { + number: '2', + endTime: '', + }, + ]; + let watchList = [ + { + number: '0', + endTime: '23:30', + }, + { + number: '1', + endTime: '20:30', + }, + { + number: '2', + endTime: '', + }, + ]; + let cleanedList = watchList; + expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual( + cleanedList, + ); - watchList = [ - { - number: "0", - endTime: "23:30", - }, - { - number: "1", - endTime: "20:30", - }, - { - number: "2", - endTime: "15:30", - }, - ]; - cleanedList = [ - { - number: "0", - endTime: "23:30", - }, - { - number: "1", - endTime: "20:30", - }, - ]; - expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); + watchList = [ + { + number: '0', + endTime: '23:30', + }, + { + number: '1', + endTime: '20:30', + }, + { + number: '2', + endTime: '15:30', + }, + ]; + cleanedList = [ + { + number: '0', + endTime: '23:30', + }, + { + number: '1', + endTime: '20:30', + }, + ]; + expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual( + cleanedList, + ); - watchList = [ - { - number: "0", - endTime: "23:30", - }, - { - number: "1", - endTime: "20:31", - }, - { - number: "3", - endTime: "15:30", - }, - ]; - cleanedList = [ - { - number: "0", - endTime: "23:30", - }, - ]; - expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); -}); \ No newline at end of file + watchList = [ + { + number: '0', + endTime: '23:30', + }, + { + number: '1', + endTime: '20:31', + }, + { + number: '3', + endTime: '15:30', + }, + ]; + cleanedList = [ + { + number: '0', + endTime: '23:30', + }, + ]; + expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual( + cleanedList, + ); +}); diff --git a/__tests__/utils/WebData.js b/__tests__/utils/WebData.js index 9f8fb5b..33e0c58 100644 --- a/__tests__/utils/WebData.js +++ b/__tests__/utils/WebData.js @@ -1,45 +1,47 @@ -import React from 'react'; -import {isResponseValid} from "../../src/utils/WebData"; +/* eslint-disable */ -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', () => { - let json = { - error: 0, - data: {} - }; - expect(isResponseValid(json)).toBeTrue(); - json = { - error: 1, - data: {} - }; - expect(isResponseValid(json)).toBeTrue(); - json = { - error: 50, - data: {} - }; - expect(isResponseValid(json)).toBeTrue(); - json = { - error: 50, - data: {truc: 'machin'} - }; - expect(isResponseValid(json)).toBeTrue(); - json = { - message: 'coucou' - }; - expect(isResponseValid(json)).toBeFalse(); - json = { - error: 'coucou', - data: {truc: 'machin'} - }; - expect(isResponseValid(json)).toBeFalse(); - json = { - error: 0, - data: 'coucou' - }; - expect(isResponseValid(json)).toBeFalse(); - json = { - error: 0, - }; - expect(isResponseValid(json)).toBeFalse(); + let json = { + error: 0, + data: {}, + }; + expect(isApiResponseValid(json)).toBeTrue(); + json = { + error: 1, + data: {}, + }; + expect(isApiResponseValid(json)).toBeTrue(); + json = { + error: 50, + data: {}, + }; + expect(isApiResponseValid(json)).toBeTrue(); + json = { + error: 50, + data: {truc: 'machin'}, + }; + expect(isApiResponseValid(json)).toBeTrue(); + json = { + message: 'coucou', + }; + expect(isApiResponseValid(json)).toBeFalse(); + json = { + error: 'coucou', + data: {truc: 'machin'}, + }; + expect(isApiResponseValid(json)).toBeFalse(); + json = { + error: 0, + data: 'coucou', + }; + expect(isApiResponseValid(json)).toBeFalse(); + json = { + error: 0, + }; + expect(isApiResponseValid(json)).toBeFalse(); }); diff --git a/src/screens/Game/__tests__/GridManager.test.js b/src/screens/Game/__tests__/GridManager.test.js index 21a687f..3f24341 100644 --- a/src/screens/Game/__tests__/GridManager.test.js +++ b/src/screens/Game/__tests__/GridManager.test.js @@ -1,103 +1,106 @@ +/* eslint-disable */ + import React from 'react'; -import GridManager from "../logic/GridManager"; -import ScoreManager from "../logic/ScoreManager"; -import Piece from "../logic/Piece"; +import GridManager from '../logic/GridManager'; +import ScoreManager from '../logic/ScoreManager'; +import Piece from '../logic/Piece'; let colors = { - tetrisBackground: "#000002" + tetrisBackground: '#000002', }; -jest.mock("../ScoreManager"); +jest.mock('../ScoreManager'); afterAll(() => { - jest.restoreAllMocks(); + jest.restoreAllMocks(); }); - test('getEmptyLine', () => { - let g = new GridManager(2, 2, colors); - expect(g.getEmptyLine(2)).toStrictEqual([ - {color: colors.tetrisBackground, isEmpty: true}, - {color: colors.tetrisBackground, isEmpty: true}, - ]); + let g = new GridManager(2, 2, colors); + expect(g.getEmptyLine(2)).toStrictEqual([ + {color: colors.tetrisBackground, isEmpty: true}, + {color: colors.tetrisBackground, isEmpty: true}, + ]); - expect(g.getEmptyLine(-1)).toStrictEqual([]); + expect(g.getEmptyLine(-1)).toStrictEqual([]); }); test('getEmptyGrid', () => { - let g = new GridManager(2, 2, colors); - 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}, - ], - ]); + let g = new GridManager(2, 2, colors); + 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}, + ], + ]); - expect(g.getEmptyGrid(-1, 2)).toStrictEqual([]); - expect(g.getEmptyGrid(2, -1)).toStrictEqual([[], []]); + expect(g.getEmptyGrid(-1, 2)).toStrictEqual([]); + expect(g.getEmptyGrid(2, -1)).toStrictEqual([[], []]); }); test('getLinesToClear', () => { - let g = new GridManager(2, 2, colors); - g.getCurrentGrid()[0][0].isEmpty = false; - g.getCurrentGrid()[0][1].isEmpty = false; - let coord = [{x: 1, y: 0}]; - expect(g.getLinesToClear(coord)).toStrictEqual([0]); + let g = new GridManager(2, 2, colors); + g.getCurrentGrid()[0][0].isEmpty = false; + g.getCurrentGrid()[0][1].isEmpty = false; + let coord = [{x: 1, y: 0}]; + expect(g.getLinesToClear(coord)).toStrictEqual([0]); - g.getCurrentGrid()[0][0].isEmpty = true; - g.getCurrentGrid()[0][1].isEmpty = true; - g.getCurrentGrid()[1][0].isEmpty = false; - g.getCurrentGrid()[1][1].isEmpty = false; - expect(g.getLinesToClear(coord)).toStrictEqual([]); - coord = [{x: 1, y: 1}]; - expect(g.getLinesToClear(coord)).toStrictEqual([1]); + g.getCurrentGrid()[0][0].isEmpty = true; + g.getCurrentGrid()[0][1].isEmpty = true; + g.getCurrentGrid()[1][0].isEmpty = false; + g.getCurrentGrid()[1][1].isEmpty = false; + expect(g.getLinesToClear(coord)).toStrictEqual([]); + coord = [{x: 1, y: 1}]; + expect(g.getLinesToClear(coord)).toStrictEqual([1]); }); test('clearLines', () => { - let g = new GridManager(2, 2, colors); - let grid = [ - [ - {color: colors.tetrisBackground, isEmpty: true}, - {color: colors.tetrisBackground, isEmpty: true}, - ], - [ - {color: '0', isEmpty: true}, - {color: '0', isEmpty: true}, - ], - ]; - g.getCurrentGrid()[1][0].color = '0'; - g.getCurrentGrid()[1][1].color = '0'; - expect(g.getCurrentGrid()).toStrictEqual(grid); - let scoreManager = new ScoreManager(); - g.clearLines([1], scoreManager); - grid = [ - [ - {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); + let g = new GridManager(2, 2, colors); + let grid = [ + [ + {color: colors.tetrisBackground, isEmpty: true}, + {color: colors.tetrisBackground, isEmpty: true}, + ], + [ + {color: '0', isEmpty: true}, + {color: '0', isEmpty: true}, + ], + ]; + g.getCurrentGrid()[1][0].color = '0'; + g.getCurrentGrid()[1][1].color = '0'; + expect(g.getCurrentGrid()).toStrictEqual(grid); + let scoreManager = new ScoreManager(); + g.clearLines([1], scoreManager); + grid = [ + [ + {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); }); test('freezeTetromino', () => { - let g = new GridManager(2, 2, colors); - let spy1 = jest.spyOn(GridManager.prototype, 'getLinesToClear') - .mockImplementation(() => {}); - let spy2 = jest.spyOn(GridManager.prototype, 'clearLines') - .mockImplementation(() => {}); - g.freezeTetromino(new Piece({}), null); + let g = new GridManager(2, 2, colors); + let spy1 = jest + .spyOn(GridManager.prototype, 'getLinesToClear') + .mockImplementation(() => {}); + let spy2 = jest + .spyOn(GridManager.prototype, 'clearLines') + .mockImplementation(() => {}); + g.freezeTetromino(new Piece({}), null); - expect(spy1).toHaveBeenCalled(); - expect(spy2).toHaveBeenCalled(); + expect(spy1).toHaveBeenCalled(); + expect(spy2).toHaveBeenCalled(); - spy1.mockRestore(); - spy2.mockRestore(); + spy1.mockRestore(); + spy2.mockRestore(); }); diff --git a/src/screens/Game/__tests__/Piece.test.js b/src/screens/Game/__tests__/Piece.test.js index e2ca450..65d0133 100644 --- a/src/screens/Game/__tests__/Piece.test.js +++ b/src/screens/Game/__tests__/Piece.test.js @@ -1,155 +1,186 @@ +/* eslint-disable */ + import React from 'react'; -import Piece from "../logic/Piece"; -import ShapeI from "../Shapes/ShapeI"; +import Piece from '../logic/Piece'; +import ShapeI from '../Shapes/ShapeI'; let colors = { - tetrisI: "#000001", - tetrisBackground: "#000002" + tetrisI: '#000001', + tetrisBackground: '#000002', }; -jest.mock("../Shapes/ShapeI"); +jest.mock('../Shapes/ShapeI'); beforeAll(() => { - jest.spyOn(Piece.prototype, 'getRandomShape') - .mockImplementation((colors: Object) => {return new ShapeI(colors);}); + jest + .spyOn(Piece.prototype, 'getRandomShape') + .mockImplementation((colors: Object) => { + return new ShapeI(colors); + }); }); afterAll(() => { - jest.restoreAllMocks(); + jest.restoreAllMocks(); }); test('isPositionValid', () => { - let x = 0; - let y = 0; - let spy = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates') - .mockImplementation(() => {return [{x: x, y: y}];}); - let grid = [ - [{isEmpty: true}, {isEmpty: true}], - [{isEmpty: true}, {isEmpty: false}], - ]; - let size = 2; + let x = 0; + let y = 0; + let spy = jest + .spyOn(ShapeI.prototype, 'getCellsCoordinates') + .mockImplementation(() => { + return [{x: x, y: y}]; + }); + let grid = [ + [{isEmpty: true}, {isEmpty: true}], + [{isEmpty: true}, {isEmpty: false}], + ]; + let size = 2; - let p = new Piece(colors); - expect(p.isPositionValid(grid, size, size)).toBeTrue(); - x = 1; y = 0; - expect(p.isPositionValid(grid, size, size)).toBeTrue(); - x = 0; y = 1; - expect(p.isPositionValid(grid, size, size)).toBeTrue(); - x = 1; y = 1; - expect(p.isPositionValid(grid, size, size)).toBeFalse(); - x = 2; y = 0; - expect(p.isPositionValid(grid, size, size)).toBeFalse(); - x = -1; 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(); + let p = new Piece(colors); + expect(p.isPositionValid(grid, size, size)).toBeTrue(); + x = 1; + y = 0; + expect(p.isPositionValid(grid, size, size)).toBeTrue(); + x = 0; + y = 1; + expect(p.isPositionValid(grid, size, size)).toBeTrue(); + x = 1; + y = 1; + expect(p.isPositionValid(grid, size, size)).toBeFalse(); + x = 2; + y = 0; + expect(p.isPositionValid(grid, size, size)).toBeFalse(); + x = -1; + 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', () => { - let p = new Piece(colors); - const callbackMock = jest.fn(); - let isValid = true; - let spy1 = jest.spyOn(Piece.prototype, 'isPositionValid') - .mockImplementation(() => {return isValid;}); - let spy2 = jest.spyOn(Piece.prototype, 'removeFromGrid') - .mockImplementation(() => {}); - let spy3 = jest.spyOn(Piece.prototype, 'toGrid') - .mockImplementation(() => {}); + let p = new Piece(colors); + const callbackMock = jest.fn(); + let isValid = true; + let spy1 = jest + .spyOn(Piece.prototype, 'isPositionValid') + .mockImplementation(() => { + return isValid; + }); + 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(); - isValid = false; - expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse(); - isValid = true; - expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue(); - expect(callbackMock).toBeCalledTimes(0); + expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeTrue(); + isValid = false; + expect(p.tryMove(-1, 0, null, null, null, callbackMock)).toBeFalse(); + isValid = true; + expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeTrue(); + expect(callbackMock).toBeCalledTimes(0); - isValid = false; - expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse(); - expect(callbackMock).toBeCalledTimes(1); + isValid = false; + expect(p.tryMove(0, 1, null, null, null, callbackMock)).toBeFalse(); + expect(callbackMock).toBeCalledTimes(1); - expect(spy2).toBeCalledTimes(4); - expect(spy3).toBeCalledTimes(4); + expect(spy2).toBeCalledTimes(4); + expect(spy3).toBeCalledTimes(4); - spy1.mockRestore(); - spy2.mockRestore(); - spy3.mockRestore(); + spy1.mockRestore(); + spy2.mockRestore(); + spy3.mockRestore(); }); test('tryRotate', () => { - let p = new Piece(colors); - let isValid = true; - let spy1 = jest.spyOn(Piece.prototype, 'isPositionValid') - .mockImplementation(() => {return isValid;}); - let spy2 = jest.spyOn(Piece.prototype, 'removeFromGrid') - .mockImplementation(() => {}); - let spy3 = jest.spyOn(Piece.prototype, 'toGrid') - .mockImplementation(() => {}); + let p = new Piece(colors); + let isValid = true; + let spy1 = jest + .spyOn(Piece.prototype, 'isPositionValid') + .mockImplementation(() => { + return isValid; + }); + let spy2 = jest + .spyOn(Piece.prototype, 'removeFromGrid') + .mockImplementation(() => {}); + let spy3 = jest.spyOn(Piece.prototype, 'toGrid').mockImplementation(() => {}); - expect(p.tryRotate( null, null, null)).toBeTrue(); - isValid = false; - expect(p.tryRotate( null, null, null)).toBeFalse(); + expect(p.tryRotate(null, null, null)).toBeTrue(); + isValid = false; + expect(p.tryRotate(null, null, null)).toBeFalse(); - expect(spy2).toBeCalledTimes(2); - expect(spy3).toBeCalledTimes(2); + expect(spy2).toBeCalledTimes(2); + expect(spy3).toBeCalledTimes(2); - spy1.mockRestore(); - spy2.mockRestore(); - spy3.mockRestore(); + spy1.mockRestore(); + spy2.mockRestore(); + spy3.mockRestore(); }); - test('toGrid', () => { - let x = 0; - let y = 0; - let spy1 = jest.spyOn(ShapeI.prototype, 'getCellsCoordinates') - .mockImplementation(() => {return [{x: x, y: y}];}); - let spy2 = jest.spyOn(ShapeI.prototype, 'getColor') - .mockImplementation(() => {return colors.tetrisI;}); - let grid = [ - [{isEmpty: true}, {isEmpty: true}], - [{isEmpty: true}, {isEmpty: true}], - ]; - let expectedGrid = [ - [{color: colors.tetrisI, isEmpty: false}, {isEmpty: true}], - [{isEmpty: true}, {isEmpty: true}], - ]; + let x = 0; + let y = 0; + let spy1 = jest + .spyOn(ShapeI.prototype, 'getCellsCoordinates') + .mockImplementation(() => { + return [{x: x, y: y}]; + }); + let spy2 = jest.spyOn(ShapeI.prototype, 'getColor').mockImplementation(() => { + return colors.tetrisI; + }); + let grid = [ + [{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); - p.toGrid(grid, true); - expect(grid).toStrictEqual(expectedGrid); + let p = new Piece(colors); + p.toGrid(grid, true); + expect(grid).toStrictEqual(expectedGrid); - spy1.mockRestore(); - spy2.mockRestore(); + spy1.mockRestore(); + spy2.mockRestore(); }); test('removeFromGrid', () => { - let gridOld = [ - [ - {color: colors.tetrisI, isEmpty: false}, - {color: colors.tetrisI, isEmpty: false}, - {color: colors.tetrisBackground, isEmpty: true}, - ], - ]; - let gridNew = [ - [ - {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 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); - p.removeFromGrid(gridOld); - expect(gridOld).toStrictEqual(gridNew); + let gridOld = [ + [ + {color: colors.tetrisI, isEmpty: false}, + {color: colors.tetrisI, isEmpty: false}, + {color: colors.tetrisBackground, isEmpty: true}, + ], + ]; + let gridNew = [ + [ + {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 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); + p.removeFromGrid(gridOld); + expect(gridOld).toStrictEqual(gridNew); - spy1.mockRestore(); - spy2.mockRestore(); + spy1.mockRestore(); + spy2.mockRestore(); }); diff --git a/src/screens/Game/__tests__/ScoreManager.test.js b/src/screens/Game/__tests__/ScoreManager.test.js index aba2df8..eec3417 100644 --- a/src/screens/Game/__tests__/ScoreManager.test.js +++ b/src/screens/Game/__tests__/ScoreManager.test.js @@ -1,71 +1,72 @@ -import React from 'react'; -import ScoreManager from "../logic/ScoreManager"; +/* eslint-disable */ +import React from 'react'; +import ScoreManager from '../logic/ScoreManager'; test('incrementScore', () => { - let scoreManager = new ScoreManager(); - expect(scoreManager.getScore()).toBe(0); - scoreManager.incrementScore(); - expect(scoreManager.getScore()).toBe(1); + let scoreManager = new ScoreManager(); + expect(scoreManager.getScore()).toBe(0); + scoreManager.incrementScore(); + expect(scoreManager.getScore()).toBe(1); }); test('addLinesRemovedPoints', () => { - let scoreManager = new ScoreManager(); - scoreManager.addLinesRemovedPoints(0); - scoreManager.addLinesRemovedPoints(5); - expect(scoreManager.getScore()).toBe(0); - expect(scoreManager.getLevelProgression()).toBe(0); + let scoreManager = new ScoreManager(); + scoreManager.addLinesRemovedPoints(0); + scoreManager.addLinesRemovedPoints(5); + expect(scoreManager.getScore()).toBe(0); + expect(scoreManager.getLevelProgression()).toBe(0); - scoreManager.addLinesRemovedPoints(1); - expect(scoreManager.getScore()).toBe(40); - expect(scoreManager.getLevelProgression()).toBe(1); + scoreManager.addLinesRemovedPoints(1); + expect(scoreManager.getScore()).toBe(40); + expect(scoreManager.getLevelProgression()).toBe(1); - scoreManager.addLinesRemovedPoints(2); - expect(scoreManager.getScore()).toBe(140); - expect(scoreManager.getLevelProgression()).toBe(4); + scoreManager.addLinesRemovedPoints(2); + expect(scoreManager.getScore()).toBe(140); + expect(scoreManager.getLevelProgression()).toBe(4); - scoreManager.addLinesRemovedPoints(3); - expect(scoreManager.getScore()).toBe(440); - expect(scoreManager.getLevelProgression()).toBe(9); + scoreManager.addLinesRemovedPoints(3); + expect(scoreManager.getScore()).toBe(440); + expect(scoreManager.getLevelProgression()).toBe(9); - scoreManager.addLinesRemovedPoints(4); - expect(scoreManager.getScore()).toBe(1640); - expect(scoreManager.getLevelProgression()).toBe(17); + scoreManager.addLinesRemovedPoints(4); + expect(scoreManager.getScore()).toBe(1640); + expect(scoreManager.getLevelProgression()).toBe(17); }); test('canLevelUp', () => { - let scoreManager = new ScoreManager(); - expect(scoreManager.canLevelUp()).toBeFalse(); - expect(scoreManager.getLevel()).toBe(0); - expect(scoreManager.getLevelProgression()).toBe(0); + let scoreManager = new ScoreManager(); + expect(scoreManager.canLevelUp()).toBeFalse(); + expect(scoreManager.getLevel()).toBe(0); + expect(scoreManager.getLevelProgression()).toBe(0); - scoreManager.addLinesRemovedPoints(1); - expect(scoreManager.canLevelUp()).toBeTrue(); - expect(scoreManager.getLevel()).toBe(1); - expect(scoreManager.getLevelProgression()).toBe(1); + scoreManager.addLinesRemovedPoints(1); + expect(scoreManager.canLevelUp()).toBeTrue(); + expect(scoreManager.getLevel()).toBe(1); + expect(scoreManager.getLevelProgression()).toBe(1); - scoreManager.addLinesRemovedPoints(1); - expect(scoreManager.canLevelUp()).toBeFalse(); - expect(scoreManager.getLevel()).toBe(1); - expect(scoreManager.getLevelProgression()).toBe(2); + scoreManager.addLinesRemovedPoints(1); + expect(scoreManager.canLevelUp()).toBeFalse(); + expect(scoreManager.getLevel()).toBe(1); + expect(scoreManager.getLevelProgression()).toBe(2); - scoreManager.addLinesRemovedPoints(2); - expect(scoreManager.canLevelUp()).toBeFalse(); - expect(scoreManager.getLevel()).toBe(1); - expect(scoreManager.getLevelProgression()).toBe(5); + scoreManager.addLinesRemovedPoints(2); + expect(scoreManager.canLevelUp()).toBeFalse(); + expect(scoreManager.getLevel()).toBe(1); + expect(scoreManager.getLevelProgression()).toBe(5); - scoreManager.addLinesRemovedPoints(1); - expect(scoreManager.canLevelUp()).toBeTrue(); - expect(scoreManager.getLevel()).toBe(2); - expect(scoreManager.getLevelProgression()).toBe(1); + scoreManager.addLinesRemovedPoints(1); + expect(scoreManager.canLevelUp()).toBeTrue(); + expect(scoreManager.getLevel()).toBe(2); + expect(scoreManager.getLevelProgression()).toBe(1); - scoreManager.addLinesRemovedPoints(4); - expect(scoreManager.canLevelUp()).toBeFalse(); - expect(scoreManager.getLevel()).toBe(2); - expect(scoreManager.getLevelProgression()).toBe(9); + scoreManager.addLinesRemovedPoints(4); + expect(scoreManager.canLevelUp()).toBeFalse(); + expect(scoreManager.getLevel()).toBe(2); + expect(scoreManager.getLevelProgression()).toBe(9); - scoreManager.addLinesRemovedPoints(2); - expect(scoreManager.canLevelUp()).toBeTrue(); - expect(scoreManager.getLevel()).toBe(3); - expect(scoreManager.getLevelProgression()).toBe(2); + scoreManager.addLinesRemovedPoints(2); + expect(scoreManager.canLevelUp()).toBeTrue(); + expect(scoreManager.getLevel()).toBe(3); + expect(scoreManager.getLevelProgression()).toBe(2); }); diff --git a/src/screens/Game/__tests__/Shape.test.js b/src/screens/Game/__tests__/Shape.test.js index 3a4537e..9f7bda1 100644 --- a/src/screens/Game/__tests__/Shape.test.js +++ b/src/screens/Game/__tests__/Shape.test.js @@ -1,104 +1,106 @@ +/* eslint-disable */ + import React from 'react'; -import BaseShape from "../Shapes/BaseShape"; -import ShapeI from "../Shapes/ShapeI"; +import BaseShape from '../Shapes/BaseShape'; +import ShapeI from '../Shapes/ShapeI'; const colors = { - tetrisI: '#000001', - tetrisO: '#000002', - tetrisT: '#000003', - tetrisS: '#000004', - tetrisZ: '#000005', - tetrisJ: '#000006', - tetrisL: '#000007', + tetrisI: '#000001', + tetrisO: '#000002', + tetrisT: '#000003', + tetrisS: '#000004', + tetrisZ: '#000005', + tetrisJ: '#000006', + tetrisL: '#000007', }; test('constructor', () => { - expect(() => new BaseShape()).toThrow(Error); + expect(() => new BaseShape()).toThrow(Error); - let T = new ShapeI(colors); - expect(T.position.y).toBe(0); - expect(T.position.x).toBe(3); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); - expect(T.getColor()).toBe(colors.tetrisI); + let T = new ShapeI(colors); + expect(T.position.y).toBe(0); + expect(T.position.x).toBe(3); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); + expect(T.getColor()).toBe(colors.tetrisI); }); -test("move", () => { - let T = new ShapeI(colors); - T.move(0, 1); - expect(T.position.x).toBe(3); - expect(T.position.y).toBe(1); - T.move(1, 0); - expect(T.position.x).toBe(4); - expect(T.position.y).toBe(1); - T.move(1, 1); - expect(T.position.x).toBe(5); - expect(T.position.y).toBe(2); - T.move(2, 2); - expect(T.position.x).toBe(7); - expect(T.position.y).toBe(4); - T.move(-1, -1); - expect(T.position.x).toBe(6); - expect(T.position.y).toBe(3); +test('move', () => { + let T = new ShapeI(colors); + T.move(0, 1); + expect(T.position.x).toBe(3); + expect(T.position.y).toBe(1); + T.move(1, 0); + expect(T.position.x).toBe(4); + expect(T.position.y).toBe(1); + T.move(1, 1); + expect(T.position.x).toBe(5); + expect(T.position.y).toBe(2); + T.move(2, 2); + expect(T.position.x).toBe(7); + expect(T.position.y).toBe(4); + T.move(-1, -1); + expect(T.position.x).toBe(6); + expect(T.position.y).toBe(3); }); test('rotate', () => { - let T = new ShapeI(colors); - T.rotate(true); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]); - T.rotate(true); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[2]); - T.rotate(true); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[3]); - T.rotate(true); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); - T.rotate(false); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[3]); - T.rotate(false); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[2]); - T.rotate(false); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]); - T.rotate(false); - expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); + let T = new ShapeI(colors); + T.rotate(true); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]); + T.rotate(true); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[2]); + T.rotate(true); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[3]); + T.rotate(true); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); + T.rotate(false); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[3]); + T.rotate(false); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[2]); + T.rotate(false); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[1]); + T.rotate(false); + expect(T.getCurrentShape()).toStrictEqual(T.getShapes()[0]); }); test('getCellsCoordinates', () => { - let T = new ShapeI(colors); - expect(T.getCellsCoordinates(false)).toStrictEqual([ - {x: 0, y: 1}, - {x: 1, y: 1}, - {x: 2, y: 1}, - {x: 3, y: 1}, - ]); - expect(T.getCellsCoordinates(true)).toStrictEqual([ - {x: 3, y: 1}, - {x: 4, y: 1}, - {x: 5, y: 1}, - {x: 6, y: 1}, - ]); - T.move(1, 1); - expect(T.getCellsCoordinates(false)).toStrictEqual([ - {x: 0, y: 1}, - {x: 1, y: 1}, - {x: 2, y: 1}, - {x: 3, y: 1}, - ]); - expect(T.getCellsCoordinates(true)).toStrictEqual([ - {x: 4, y: 2}, - {x: 5, y: 2}, - {x: 6, y: 2}, - {x: 7, y: 2}, - ]); - T.rotate(true); - expect(T.getCellsCoordinates(false)).toStrictEqual([ - {x: 2, y: 0}, - {x: 2, y: 1}, - {x: 2, y: 2}, - {x: 2, y: 3}, - ]); - expect(T.getCellsCoordinates(true)).toStrictEqual([ - {x: 6, y: 1}, - {x: 6, y: 2}, - {x: 6, y: 3}, - {x: 6, y: 4}, - ]); + let T = new ShapeI(colors); + expect(T.getCellsCoordinates(false)).toStrictEqual([ + {x: 0, y: 1}, + {x: 1, y: 1}, + {x: 2, y: 1}, + {x: 3, y: 1}, + ]); + expect(T.getCellsCoordinates(true)).toStrictEqual([ + {x: 3, y: 1}, + {x: 4, y: 1}, + {x: 5, y: 1}, + {x: 6, y: 1}, + ]); + T.move(1, 1); + expect(T.getCellsCoordinates(false)).toStrictEqual([ + {x: 0, y: 1}, + {x: 1, y: 1}, + {x: 2, y: 1}, + {x: 3, y: 1}, + ]); + expect(T.getCellsCoordinates(true)).toStrictEqual([ + {x: 4, y: 2}, + {x: 5, y: 2}, + {x: 6, y: 2}, + {x: 7, y: 2}, + ]); + T.rotate(true); + expect(T.getCellsCoordinates(false)).toStrictEqual([ + {x: 2, y: 0}, + {x: 2, y: 1}, + {x: 2, y: 2}, + {x: 2, y: 3}, + ]); + expect(T.getCellsCoordinates(true)).toStrictEqual([ + {x: 6, y: 1}, + {x: 6, y: 2}, + {x: 6, y: 3}, + {x: 6, y: 4}, + ]); });