SHARE
TWEET

Untitled

a guest Jul 24th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import AbstractModel from '../models/Abstract';
  2. import AbstractCollection from '../collections/Abstract';
  3. import ajaxAdapterMock from './mocks/service/AjaxAdapter';
  4. import { store } from '../setupTests';
  5. import entityReducer from '../reducers/entity';
  6. import { combineReducers } from 'redux';
  7. import StorageError from '../errors/StorageError';
  8. import ROLES from '../rolesConfig';
  9.  
  10.  
  11. class EmployeeModel extends AbstractModel {
  12.     static get urlRoot () {
  13.         return 'employees';
  14.     }
  15.  
  16.     static get ENTITY_TYPE () {
  17.         return 'employees';
  18.     }
  19. }
  20.  
  21. class EmployeesCollection extends AbstractCollection {
  22.     static get model () {
  23.         return EmployeeModel;
  24.     }
  25.     getModels() {
  26.         return this.models;
  27.     }
  28. }
  29.  
  30.  
  31.  
  32. class RoleModel extends AbstractModel {
  33.     static get urlRoot () {
  34.         return 'roles';
  35.     }
  36.  
  37.     static get ENTITY_TYPE () {
  38.         return 'roles';
  39.     }
  40. }
  41.  
  42. class RolesCollection extends AbstractCollection {
  43.     static get model () {
  44.         return RoleModel;
  45.     }
  46.     getModels() {
  47.         return this.models;
  48.     }
  49. }
  50.  
  51. export const generateEmployeeModel = (empty = true, count) => {
  52.     return new EmployeeModel(empty ? {} : {
  53.         aseFunction: 1,
  54.         displayName: null,
  55.         status: 'enabled',
  56.         endDate: null,
  57.         firstName: "Admin" + count,
  58.         hireDate: "2018-07-31T00:00:00+03:00",
  59.         id: "04637ac1-6e51-11e8-9e8d-1256795c62d" + (count+ 5),
  60.         lastName: "Mirro",
  61.         position:{
  62.             id: 1,
  63.             name: "Senior System Administrator",
  64.             shortName: "SSA"
  65.         }
  66.     });
  67. };
  68.  
  69. export const generateEmployeesCollection = (nr = 0) => {
  70.     const models = [];
  71.     const collection = new EmployeesCollection();
  72.     for (let i = 0; i < nr; i++) {
  73.         models.push(generateEmployeeModel(false, i+1));
  74.         collection.set(i, models[i]);
  75.     }
  76.  
  77.     return collection;
  78. };
  79.  
  80. export const generateCollection = (nr = 0) => {
  81.     const models = [];
  82.     for (let i = 0; i < nr; i++) {
  83.         models.push(generateRole(false, i+1));
  84.     }
  85.     return models;
  86. };
  87.  
  88. export const generateRole = (empty = true, count) => {
  89.     const randomRole = Object.keys(ROLES).filter(role => ROLES[role] === count+1);
  90.  
  91.     return new RoleModel(empty ? {} : {
  92.         'description': 'Lorem Ipsum ' + count,
  93.         'name': 'Role Name ' + count,
  94.         'enabled': 1,
  95.         'id': count,
  96.         'role': randomRole,
  97.         'userCount': count + 3,
  98.     });
  99. };
  100.  
  101.  
  102.  
  103. export const setupFailGetDetailsCall = () => {
  104.     const headers = new Headers();
  105.     headers.append('Content-Type', 'application/json');
  106.     global.fetch.mockResponseOnce('{}',
  107.         {'status': 404, headers, statusText: 'Not found'});
  108.     const err = new StorageError();
  109.     err.setData({status: 404});
  110.     ajaxAdapterMock.handleResponse.mockReturnValue(Promise.reject(err));
  111. };
  112.  
  113. export const setupGetListSuccessCall = () => {
  114.     const headers = new Headers(),
  115.         data = [];
  116.     headers.append('Content-Type', 'application/json');
  117.     for (let i = 0; i < 20; i++) {
  118.         data.push(generateRole(false).toJson());
  119.     }
  120.     const body = JSON.stringify({
  121.         error: null,
  122.         data,
  123.         metadata: null,
  124.     });
  125.     global.fetch.mockResponseOnce(body, {'status': 200, headers});
  126.     ajaxAdapterMock.handleResponse.mockReturnValue(Promise.resolve({
  127.         body: JSON.parse(body),
  128.     }));
  129. };
  130.  
  131. export const setupGetList400Call = () => {
  132.     const headers = new Headers();
  133.     headers.append('Content-Type', 'application/json');
  134.     global.fetch.mockResponseOnce('{}',
  135.         {'status': 400, headers, statusText: 'Bad request'});
  136.     const err = new StorageError();
  137.     err.setData({status: 400});
  138.     ajaxAdapterMock.handleResponse.mockReturnValue(Promise.reject(err));
  139. };
  140.  
  141. const reducers = combineReducers({
  142.     entity: entityReducer
  143. });
  144. store.replaceReducer(reducers);
  145. export { RoleModel, RolesCollection };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top