Skip to content

Commit

Permalink
unitest
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbedouret committed Jun 23, 2019
1 parent 1b3aa60 commit 3e1cbfa
Show file tree
Hide file tree
Showing 17 changed files with 612 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cboard.njsproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LastActiveSolutionConfig>Debug|Any CPU</LastActiveSolutionConfig>
</PropertyGroup>
</Project>
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"enzyme-adapter-react-16": "^1.13.2",
"enzyme-to-json": "3.3.5",
"husky": "^1.1.4",
"jest-mock-axios": "3.0.0",
"lint-staged": "^7.3.0",
"nock": "^10.0.2",
"prettier": "1.15.3",
Expand Down Expand Up @@ -104,7 +105,9 @@
"!src/components/**/index.js",
"!src/providers/**/index.js",
"!src/components/**/*.constants.js",
"!src/components/**/*.messages.js"
"!src/components/**/*.messages.js",
"!src/components/**/*.container.js",
"!src/components/**/*.helpers.js"
]
},
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/__mocks__/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import mockAxios from 'jest-mock-axios';
export default mockAxios;
24 changes: 24 additions & 0 deletions src/__mocks__/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

let initialState;
initialState = {
app: {
userData: {
authToken: "eyJhbGciOiJIUtI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFybCI6ImFueXRoaW5nQGNib2FyZC5pbyIsImlkIjoiNWJjZmE0ZWQ0OTRiMjAwMDBmOGFiOThiIiwiaXNzdWVyIjoiY2JvYXJkLmlvIiwiaWF0IjoxNTU5NzU1NTU3fQ.2ENI4GyaHwV1B-Pifi0ZUKqyGcjTSLDV0UoPKUY99bo",
email: "[email protected]",
id: "5bcfa4ed494b20000f8ab98b",
lastlogin: "2018-10-23T22:47:09.367Z",
locale: "en-US",
name: "martin bedouret",
provider: "",
role: "user"
}
}
};
const store = mockStore(initialState);

export const getStore = () => store;
14 changes: 14 additions & 0 deletions src/__test__/helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as helpers from '../helpers';
describe('helpers', () => {

it('should create a file from data', () => {
const dataUrl = "data:text/plain;charset=utf-8;base64,dGVzdGluZw==";
const file = helpers.dataURLtoFile(dataUrl, 'myfile');
expect(file.name).toBe("myfile");
});
it('should create a file with ext from data', () => {
const dataUrl = "data:text/plain;charset=utf-8;base64,dGVzdGluZw==";
const file = helpers.dataURLtoFile(dataUrl, 'myfile.txt', true);
expect(file.name).toBe('myfile.txt.plain');
});
});
21 changes: 21 additions & 0 deletions src/__test__/i18n.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as i18n from '../i18n';
describe('i18n', () => {

it('should import translation data', () => {
i18n.importTranslation('es-ES')
.then((lang) => {
expect(lang.cboard.components.WelcomeScreen.login).toBe("Iniciar sesión");
});
});
it('should strip Region Code', () => {
expect(i18n.stripRegionCode('es-ES')).toBe("es");
});
it('should normalize Language Code', () => {
expect(i18n.normalizeLanguageCode('ES-ES')).toBe("es-ES");
expect(i18n.normalizeLanguageCode('es-es')).toBe("es-ES");
});
it('should normalize Language Code for two', () => {
expect(i18n.normalizeLanguageCode('ES')).toBe("es");
expect(i18n.normalizeLanguageCode('es')).toBe("es");
});
});
9 changes: 9 additions & 0 deletions src/__test__/reducers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import createReducer from '../reducers';
describe('reducers', () => {

it('should create Reducer', () => {
const red = createReducer();
expect(red).toBeDefined;
});
});
42 changes: 42 additions & 0 deletions src/__test__/registerServiceWorker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import register from '../registerServiceWorker';
import unregister from '../registerServiceWorker';
describe('registerServiceWorker', () => {

global.navigator = Object.create(navigator);
Object.defineProperty(navigator, 'serviceWorker', {
value: { ready: false }
});
global.process.env = Object.create(process.env);
Object.defineProperty(process.env, 'NODE_ENV', {
value: 'production'
});
global.process.env = Object.create(process.env);
Object.defineProperty(process.env, 'PUBLIC_URL', {
value: 'https://app.cboard.io'
});
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
href: 'https://app.cboard.io',
origin: 'https://app.cboard.io'
},
writable: true
});

it('should register Service Worker', () => {
const serviceWorker = register(jest.fn(), jest.fn());
expect(serviceWorker).toBeDefined;
});
it('should register Service Worker localhost', () => {
Object.defineProperty(window.location, 'hostname', {
value: 'localhost'
});
const serviceWorker = register(jest.fn(), jest.fn());
expect(serviceWorker).toBeDefined;
});
it('should unregister Service Worker', () => {
const serviceWorker = unregister();
expect(serviceWorker).toBeDefined;
});
});
9 changes: 9 additions & 0 deletions src/__test__/store.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import configureStore from '../store';
describe('store', () => {

it('should configure Store', () => {
const store = configureStore();
expect(store).toBeDefined;
});
});
148 changes: 148 additions & 0 deletions src/api/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@

import API from './api';
import mockAxios from 'jest-mock-axios';

let store;
// allows us to easily return reponses and/or success/fail for a thunk that calls a service
const mockServiceCreator = (body, succeeds = true) => () =>
new Promise((resolve, reject) => {
setTimeout(() => (succeeds ? resolve(body) : reject(body)), 10);
});

describe('Cboard API calls', () => {
let initialState;
// set up a fake store for all our tests
beforeEach(() => {
});
afterEach(() => {
// cleaning up the mess left behind the previous test
mockAxios.reset();
});

it("fetches results from get language api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();

//call method
API.getLanguage('es-ES')
.then(thenFn)
.catch(catchFn);
// simulating a server response
let responseObj = { data: 'fake!' };
mockAxios.mockResponse(responseObj);
});
it("fetches results from get my boards api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();

//call method
API.getMyBoards(1, 10)
.then(thenFn)
.catch(catchFn);
});
it("fetches results from get boards api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();

//call method
API.getBoards(1)
.then(thenFn)
.catch(catchFn);
});
it("fetches results from get board api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();

//call method
API.getBoard('aaaa')
.then(thenFn)
.catch(catchFn);
});
it("fetches results from get communicators api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();

//call method
API.getCommunicators(1,10)
.then(thenFn)
.catch(catchFn);
});
it("fetches results from createBoard api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();
const board = {
"name": "board_name",
"author": "Postman Test",
"email": "[email protected]",
"format": "obf",
"tiles": [
{}
],
"caption": "string",
"isPublic": true,
"locale": "ar-SA"
}
//call method
API.createBoard(board)
.then(thenFn)
.catch(catchFn);
});
it("fetches results from createCommunicator api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();
const comm = {
"name": "comm_name",
"author": "Postman Test",
"email": "[email protected]",
"rootBoard": "obf",
"boards": [
{}
]
};
//call method
API.createCommunicator(comm)
.then(thenFn)
.catch(catchFn);
});
it("fetches results from arasaacPictogramsSearch api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();
//call method
API.arasaacPictogramsSearch('es','perro')
.then(thenFn)
.catch(catchFn);
// simulating a server response
let responseObj = { data: 'fake!' };
mockAxios.mockResponse(responseObj);
});
it("fetches results from deleteBoard api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();
//call method
API.deleteBoard('esnm')
.then(thenFn)
.catch(catchFn);
});
it("fetches results from login api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();
const user = {
"email": "[email protected]",
"password": "123456"
}
//call method
API.login(user)
.then(thenFn)
.catch(catchFn);
// simulating a server response
let responseObj = { data: 'fake!' };
mockAxios.mockResponse(responseObj);
});
it("fetches results from tawasolPictogramsSearch api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();
//call method
API.tawasolPictogramsSearch('es', 'perro')
.then(thenFn)
.catch(catchFn);
// simulating a server response
let responseObj = { data: 'fake!' };
mockAxios.mockResponse(responseObj);
});
it("fetches results from uploadFromDataURL api", () => {
let catchFn = jest.fn(), thenFn = jest.fn();
//call method
API.uploadFromDataURL('es', 'perro')
.then(thenFn)
.catch(catchFn);
});
});
38 changes: 38 additions & 0 deletions src/components/Account/Activate/__tests__/Activate.actions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

import * as ac from '../Activate.actions';
import mockAxios from 'jest-mock-axios';
import { API_URL } from '../../../../constants';

afterEach(() => {
// cleaning up the mess left behind the previous test
mockAxios.reset();
});

describe('Activate actions', () => {
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
href: 'https://app.cboard.io',
origin: 'https://app.cboard.io'
},
writable: true
});
it("fetches results from get language api", () => {

const url = 'asdf';

let catchFn = jest.fn(),
thenFn = jest.fn();

//call method
ac.activate(url)
.then(thenFn)
.catch(catchFn);
expect(mockAxios.post).toHaveBeenCalledWith(`${API_URL}/user/activate/${url}`);
// simulating a server response
let responseObj = { data: 'activated!' };
mockAxios.mockResponse(responseObj);


});
});
Loading

0 comments on commit 3e1cbfa

Please sign in to comment.