From d7a24f767c9133046febf37af78a3307f90cdc55 Mon Sep 17 00:00:00 2001 From: tomastrajan Date: Sun, 16 Jun 2019 18:06:52 +0200 Subject: [PATCH] fix(tests): rework tests o correctly use rxjs TestScheduler --- .../stock-market/stock-market.effects.spec.ts | 175 +++++++++--------- .../rtl-support/rtl-support.directive.spec.ts | 67 +++---- src/polyfills.ts | 2 + 3 files changed, 117 insertions(+), 127 deletions(-) diff --git a/src/app/examples/stock-market/stock-market.effects.spec.ts b/src/app/examples/stock-market/stock-market.effects.spec.ts index 204acb870..192f8dc38 100755 --- a/src/app/examples/stock-market/stock-market.effects.spec.ts +++ b/src/app/examples/stock-market/stock-market.effects.spec.ts @@ -14,110 +14,111 @@ import { StockMarketEffects, STOCK_MARKET_KEY } from './stock-market.effects'; import { Stock } from './stock-market.model'; import { StockMarketService } from './stock-market.service'; -const scheduler = new TestScheduler((actual, expected) => - assert.deepStrictEqual(actual, expected) -); +const symbol = 'TSLA'; describe('StockMarketEffects', () => { let localStorage: jasmine.SpyObj; let stockMarket: jasmine.SpyObj; + let scheduler; beforeEach(() => { localStorage = jasmine.createSpyObj('localStorageService', ['setItem']); stockMarket = jasmine.createSpyObj('stockMarketService', ['retrieveStock']); + scheduler = new TestScheduler((actual, expected) => + assert.deepStrictEqual(actual, expected) + ); }); - describe('retrieveStock', () => { - const symbol = 'TSLA'; - - fit('should emit ActionStockMarketRetrieveSuccess on success', () => { - scheduler.run(helpers => { - const { cold, expectObservable } = helpers; - const retrieveAction1 = new ActionStockMarketRetrieve({ - symbol - }); - const retrieveAction2 = new ActionStockMarketRetrieve({ - symbol - }); - const retrieveAction3 = new ActionStockMarketRetrieve({ - symbol - }); - const stock: Stock = { - symbol, - exchange: 'exchange', - last: '42', - ccy: 'USD', - change: 'change', - changePositive: true, - changeNegative: false, - changePercent: '2.00' - }; - const successAction = new ActionStockMarketRetrieveSuccess({ - stock - }); - const values = { - a: retrieveAction1, - b: retrieveAction2, - c: retrieveAction3, - s: successAction - }; - const source = cold('a--b--c', values); - const expected = '--s--s--s'; - const actions = new Actions(source); - - stockMarket.retrieveStock.and.returnValue(of(stock)); - - const effects = new StockMarketEffects( - actions, - localStorage, - stockMarket - ); - - expectObservable( - effects.retrieveStock({ - debounce: 20 - }) - ).toBe(expected); - - + it('should emit ActionStockMarketRetrieveSuccess on success', done => { + scheduler.run(helpers => { + const { cold, expectObservable } = helpers; + const retrieveAction1 = new ActionStockMarketRetrieve({ + symbol + }); + const retrieveAction2 = new ActionStockMarketRetrieve({ + symbol + }); + const retrieveAction3 = new ActionStockMarketRetrieve({ + symbol + }); + const stock: Stock = { + symbol, + exchange: 'exchange', + last: '42', + ccy: 'USD', + change: 'change', + changePositive: true, + changeNegative: false, + changePercent: '2.00' + }; + const successAction = new ActionStockMarketRetrieveSuccess({ + stock + }); + const values = { + a: retrieveAction1, + b: retrieveAction2, + c: retrieveAction3, + s: successAction + }; + const source = cold('a--b--c', values); + const expected = '--s--s--s'; + const actions = new Actions(source); + + stockMarket.retrieveStock.and.returnValue(of(stock)); + + const effects = new StockMarketEffects( + actions, + localStorage, + stockMarket + ); + + expectObservable( + effects.retrieveStock({ + debounce: 2 + }) + ).toBe(expected, values); + + setTimeout(() => { + expect(localStorage.setItem).toHaveBeenCalledTimes(3); expect(localStorage.setItem).toHaveBeenCalledWith(STOCK_MARKET_KEY, { symbol }); + done(); }); }); + }); - it('should emit ActionStockMarketRetrieveError on error', () => { - scheduler.run(helpers => { - const { cold, expectObservable } = helpers; - const retrieveAction = new ActionStockMarketRetrieve({ - symbol - }); - const error = 'ERROR'; - const errorAction = new ActionStockMarketRetrieveError({ - error - } as any); - const values = { - a: retrieveAction, - e: errorAction - }; - const source = cold('a', values); - const expected = '--e'; - const actions = new Actions(source); - - stockMarket.retrieveStock.and.returnValue(throwError(error)); - - const effects = new StockMarketEffects( - actions, - localStorage, - stockMarket - ); - - expectObservable( - effects.retrieveStock({ - debounce: 20 - }) - ).toBe(expected); + it('should emit ActionStockMarketRetrieveError on error', () => { + scheduler.run(helpers => { + const { cold, expectObservable } = helpers; + const retrieveAction = new ActionStockMarketRetrieve({ + symbol }); + const error = 'ERROR'; + const errorAction = new ActionStockMarketRetrieveError({ + error + } as any); + const values = { + a: retrieveAction, + e: errorAction + }; + const source = cold('--a', values); + const expected = '--e'; + const actions = new Actions(source); + + stockMarket.retrieveStock.and.returnValue(throwError(error)); + + const effects = new StockMarketEffects( + actions, + localStorage, + stockMarket + ); + + expectObservable( + effects.retrieveStock({ + debounce: 0 + }) + ).toBe(expected, values); }); }); }); diff --git a/src/app/shared/rtl-support/rtl-support.directive.spec.ts b/src/app/shared/rtl-support/rtl-support.directive.spec.ts index 0bb745744..71a2ebbc2 100755 --- a/src/app/shared/rtl-support/rtl-support.directive.spec.ts +++ b/src/app/shared/rtl-support/rtl-support.directive.spec.ts @@ -1,15 +1,10 @@ -import * as assert from 'assert'; import { By } from '@angular/platform-browser'; import { Component, DebugElement } from '@angular/core'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { TranslateModule, TranslateService } from '@ngx-translate/core'; -import { TestScheduler } from 'rxjs/testing'; import { RtlSupportDirective } from './rtl-support.directive'; - -const scheduler = new TestScheduler((actual, expected) => - assert.deepStrictEqual(actual, expected) -); +import { BehaviorSubject, of } from 'rxjs'; @Component({ template: ` @@ -25,36 +20,31 @@ describe('RtlSupportDirective', () => { let fixture: ComponentFixture; let des: DebugElement[]; // the three elements w/ the directive let bareH2: DebugElement; // the

w/o the directive + let languageSubject; beforeEach(() => { - scheduler.run(helpers => { - const { cold, flush } = helpers; - fixture = TestBed.configureTestingModule({ - imports: [TranslateModule.forRoot()], - declarations: [RtlSupportDirective, TestComponent], - providers: [ - { - provide: TranslateService, - useValue: { - currentLang: 'he', - onLangChange: cold('--x--y|', { - x: { lang: 'he' }, - y: { lang: 'de' } - }) - } + languageSubject = new BehaviorSubject({ lang: 'he' }); + fixture = TestBed.configureTestingModule({ + imports: [TranslateModule.forRoot()], + declarations: [RtlSupportDirective, TestComponent], + providers: [ + { + provide: TranslateService, + useValue: { + currentLang: 'he', + onLangChange: languageSubject.asObservable() } - ] - }).createComponent(TestComponent); + } + ] + }).createComponent(TestComponent); - flush(); // flush the observables - fixture.detectChanges(); // initial binding + fixture.detectChanges(); // initial binding - // all elements with an attached RtlDirective - des = fixture.debugElement.queryAll(By.directive(RtlSupportDirective)); + // all elements with an attached RtlDirective + des = fixture.debugElement.queryAll(By.directive(RtlSupportDirective)); - // the h2 without the RtlDirective - bareH2 = fixture.debugElement.query(By.css('h2:not([rtl])')); - }); + // the h2 without the RtlDirective + bareH2 = fixture.debugElement.query(By.css('h2:not([rtl])')); }); // color tests @@ -72,16 +62,13 @@ describe('RtlSupportDirective', () => { expect(direction).toBe('rtl'); }); - it('should set "direction" rule value to "ltr" after current language changed to NOT hebrew', () => { - scheduler.run(helpers => { - const { flush } = helpers; - flush(); // flush the observables - fixture.detectChanges(); + it('should set "direction" rule value to "ltr" after current language changed to German', () => { + languageSubject.next({ lang: 'de' }); + fixture.detectChanges(); - const textAlign = des[0].nativeElement.style.textAlign; - expect(textAlign).toBe('left'); - const direction = des[0].nativeElement.style.direction; - expect(direction).toBe('ltr'); - }); + const textAlign = des[0].nativeElement.style.textAlign; + expect(textAlign).toBe('left'); + const direction = des[0].nativeElement.style.direction; + expect(direction).toBe('ltr'); }); }); diff --git a/src/polyfills.ts b/src/polyfills.ts index 2f258e56c..2e3835139 100755 --- a/src/polyfills.ts +++ b/src/polyfills.ts @@ -60,3 +60,5 @@ import 'zone.js/dist/zone'; // Included with Angular CLI. /*************************************************************************************************** * APPLICATION IMPORTS */ + +import 'hammerjs';