Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. const { createComponentStub } = require('frint-test');
  2.  
  3. const Car = require(`${__base}/components/Car/Car`);
  4. const { prepareDisplayData } = require(`${__base}/components/Car/Car.helpers`);
  5. const createCar = require(`${__base}/test/components/car/mock`);
  6.  
  7. const CarRoot = require('../../../containers/CarRoot/CarRoot');
  8.  
  9. const { init } = require('../../../containers/CarRoot/CarRoot.actions');
  10. const { showCar } = require('../../../../carsidebar/containers/CarShow/CarShow.actions');
  11.  
  12. const mount = enzyme.mount;
  13. const render = enzyme.render;
  14.  
  15. const createCarRootStub = (initAction = init, nbLegs = 2, coreUpdater = { updateCore: () => {} }) => {
  16. return createComponentStub(CarRoot, {
  17. dispatch: {
  18. init: initAction,
  19. showCar,
  20. },
  21. factories: {
  22. assets: {
  23. url(arg) {
  24. return arg;
  25. },
  26. },
  27. externalRequest: {
  28. send(alias, method, urlPath, body) {
  29. return new Promise((resolve) => {
  30. resolve(body || '');
  31. });
  32. },
  33. get(alias, urlPath) {
  34. return this.send(alias, 'GET', urlPath);
  35. },
  36. post(alias, urlPath, data) {
  37. return this.send(alias, 'POST', urlPath, data);
  38. },
  39. },
  40. },
  41. models: {
  42. affiliate: {
  43. getAffiliateCode() {
  44. return '';
  45. },
  46. },
  47. },
  48. services: {
  49. l10ns: {
  50. l(key) {
  51. return key;
  52. },
  53. getCurrentCurrency() {
  54. return 'EUR';
  55. },
  56. getCurrentCurrencySymbol() {
  57. return '€';
  58. },
  59. getCurrentCulture() {
  60. return 'en'
  61. }
  62. },
  63. sessionStorage: {
  64. get() {
  65. return null;
  66. },
  67. set() {
  68. return null;
  69. }
  70. },
  71. widgetsMediator: {
  72. toSidebar() {},
  73. toRoot() {},
  74. },
  75. tripDataMapper: {
  76. getTripData() {
  77. return Promise.resolve({
  78. flightDetails: {
  79. Legs: Array.from({ length: nbLegs }, (value, key) => ({ // eslint-disable-line
  80. ArrivalAirport: { Code: '' },
  81. ArrivalDateTime: '',
  82. })),
  83. Fares: [{
  84. PassengerFares: [{
  85. NrOfPax: 1,
  86. }],
  87. }],
  88. },
  89. checkoutContact: {
  90. CountryCode: '',
  91. },
  92. });
  93. },
  94. },
  95. carService: {
  96. shop() {
  97. return Promise.resolve({
  98. cars: [],
  99. });
  100. },
  101. },
  102. coreUpdater,
  103. },
  104. state: s => ({
  105. ...s.carRoot,
  106. ...s.carShow,
  107. }),
  108. });
  109. };
  110.  
  111. describe('<CarRoot />', () => {
  112. it.only('should render carRoot widget after car is loaded', (done) => {
  113. const cars = [1, 2, 3].map(() => prepareDisplayData(createCar()));
  114. const CarRootStub = createCarRootStub();
  115. const wrapper = mount(<CarRootStub cars={cars} loading={false} />);
  116. setTimeout(() => {
  117. expect(wrapper.find('.car-start--state-loading')).to.have.length(0);
  118. expect(wrapper.find('.car-start--car-list')).to.have.length(1);
  119. expect(wrapper.find(Car)).to.have.property('length').to.be.equal(3);
  120. done();
  121. });
  122. });
  123.  
  124. it('should call "coreUpdater" if the amount of legs is less than 2', (done) => {
  125. const spyInit = sinon.spy();
  126.  
  127. const coreUpdater = {
  128. updateCore: () => {},
  129. };
  130.  
  131. const spyCoreUpdater = sinon.spy(coreUpdater, 'updateCore');
  132.  
  133. const CarRootStub = createCarRootStub(spyInit, 1, coreUpdater);
  134.  
  135. mount(<CarRootStub />);
  136.  
  137. setTimeout(() => {
  138. expect(spyCoreUpdater).to.have.callCount(1);
  139. expect(spyInit).to.have.callCount(0);
  140. done();
  141. });
  142. });
  143.  
  144. it('should call "coreUpdater" if the amount of legs is more than 2', (done) => {
  145. const spyInit = sinon.spy();
  146.  
  147. const coreUpdater = {
  148. updateCore: () => {},
  149. };
  150.  
  151. const spyCoreUpdater = sinon.spy(coreUpdater, 'updateCore');
  152.  
  153. const CarRootStub = createCarRootStub(spyInit, 3, coreUpdater);
  154.  
  155. mount(<CarRootStub />);
  156.  
  157. setTimeout(() => {
  158. expect(spyCoreUpdater).to.have.callCount(1);
  159. expect(spyInit).to.have.callCount(0);
  160. done();
  161. });
  162. });
  163.  
  164. it('should call "init" method right after mount', (done) => {
  165. const spy = sinon.spy();
  166. const CarRootStub = createCarRootStub(spy);
  167. mount(<CarRootStub />);
  168. // because of a promise in afterMount (tripDataMapper.getTripData()), this test is async now
  169. setTimeout(() => {
  170. expect(spy).to.have.callCount(1);
  171. done();
  172. });
  173. });
  174.  
  175. it('should render loading block if loading is in progress', () => {
  176. const CarRootStub = createCarRootStub();
  177. const wrapper = render(<CarRootStub loading />);
  178. expect(wrapper.find('.car-start--state-loading')).to.have.length(1);
  179. });
  180.  
  181. it('should render error block if loading is in progress', () => {
  182. const CarRootStub = createCarRootStub();
  183. const wrapper = render(<CarRootStub error="AN_ERROR_KEY" />);
  184. expect(wrapper.find('.car-start--state-error')).to.have.length(1);
  185. });
  186.  
  187. it('should hide if there is an error', () => {
  188. const CarRootStub = createCarRootStub();
  189. const wrapper = render(<CarRootStub loading={false} />);
  190. expect(wrapper.find('.car-start')).to.have.length(0);
  191. expect(wrapper.find('.car-start--state-loading')).to.have.length(0);
  192. expect(wrapper.find('.car-start--state-empty')).to.have.length(1);
  193. });
  194.  
  195. it('should render selected block if there is a selected car', () => {
  196. const cars = [1, 2, 3].map(() => prepareDisplayData(createCar()));
  197. const CarRootStub = createCarRootStub();
  198. const wrapper = mount(<CarRootStub cars={cars} loading={false} selectedCar={prepareDisplayData(createCar())} />);
  199. expect(wrapper.find('.car--state-selected')).to.have.length(1);
  200. });
  201. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement