Skip to content

Commit

Permalink
fix(tests): rework tests o correctly use rxjs TestScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastrajan committed Jun 16, 2019
1 parent 78946f4 commit d7a24f7
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 127 deletions.
175 changes: 88 additions & 87 deletions src/app/examples/stock-market/stock-market.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalStorageService>;
let stockMarket: jasmine.SpyObj<StockMarketService>;
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);
});
});
});
67 changes: 27 additions & 40 deletions src/app/shared/rtl-support/rtl-support.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -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: `
Expand All @@ -25,36 +20,31 @@ describe('RtlSupportDirective', () => {
let fixture: ComponentFixture<TestComponent>;
let des: DebugElement[]; // the three elements w/ the directive
let bareH2: DebugElement; // the <h2> 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
Expand All @@ -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');
});
});
2 changes: 2 additions & 0 deletions src/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ import 'zone.js/dist/zone'; // Included with Angular CLI.
/***************************************************************************************************
* APPLICATION IMPORTS
*/

import 'hammerjs';

0 comments on commit d7a24f7

Please sign in to comment.