import React from 'react'; import ConnectionManager, {ERROR_TYPE} from "../../managers/ConnectionManager"; import * as SecureStore from 'expo-secure-store'; let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native const c = ConnectionManager.getInstance(); test("connect bad credentials", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ json: () => { return { state: false, message: 'Adresse mail ou mot de passe incorrect', token: '' } }, }) }); return expect(c.connect('email', 'password')) .rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS); }); test("connect good credentials", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ json: () => { return { state: true, message: 'Connexion confirmée', token: 'token' } }, }) }); jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => { return Promise.resolve(true); }); return expect(c.connect('email', 'password')).resolves.toBeTruthy(); }); test("connect good credentials, fail save token", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ json: () => { return { state: true, message: 'Connexion confirmée', token: 'token' } }, }) }); jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => { return Promise.reject(false); }); return expect(c.connect('email', 'password')).rejects.toBe(ERROR_TYPE.SAVE_TOKEN); }); 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 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); }); test("connect bogus response 2", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ json: () => { return { state: true, message: '', } }, }) }); return expect(c.connect('email', 'password')) .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); }); test("connect bogus response 3", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ json: () => { return { state: false, message: '', } }, }) }); return expect(c.connect('email', 'password')) .rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS); }); test("connect bogus response 4", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ json: () => { return { message: '', token: '', } }, }) }); return expect(c.connect('email', 'password')) .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); }); test("connect bogus response 5", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ json: () => { return { state: true, message: 'Connexion confirmée', token: '' } }, }) }); return expect(c.connect('email', 'password')) .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR); });