Skip to content

Commit

Permalink
Merge pull request #4 from Some-of-the-things/tutorial/5-mapsplusmarbles
Browse files Browse the repository at this point in the history
add marbles tests
  • Loading branch information
Some-of-the-things authored Sep 16, 2024
2 parents 5b61004 + 64f25ad commit e588dc2
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 122 deletions.
29 changes: 0 additions & 29 deletions src/app/app.component.spec.ts

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<h1>It's All Subjective</h1>
<div>
<button (click)="defaultReplay()">Default Replay Subject</button>
<button (click)="replayNumber()">Replay with Number</button>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Maps + Marbles</h1>
<p>Check the tests in the mapsandmarbles-5.component.spec.ts for this one<p>
Empty file.
156 changes: 156 additions & 0 deletions src/app/tutorials/mapsandmarbles-5/mapsandmarbles-5.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { TestScheduler } from 'rxjs/testing';
import { map, switchMap, exhaustMap, concatMap, mergeMap } from 'rxjs';

describe('Mapsandmarbles5', () => {
let testScheduler: TestScheduler;

beforeEach(async () => {
testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
});

describe('map', () => {
it('maps values', () => {
testScheduler.run((helpers) => {
const { cold, expectObservable } = helpers;
const e1 = cold(' -a-----b---|');
const expected = '-a-----b---|';
const aCode = 'a'.charCodeAt(0);
const bCode = 'b'.charCodeAt(0);
const values = {
a: aCode,
b: bCode,
};

expectObservable(
e1.pipe(map((x: string) => x.charCodeAt(0)))).toBe(expected, values);
});
});
});

describe('switchMap', () => {
it('switches to new observable, cancelling previous one', () => {
testScheduler.run((helpers) => {
const { cold, expectObservable } = helpers;
const firstMemoryOf = {
a: 'Speaking words',
b: 'Eating solids',
c: 'Walking',
d: 'Driving',
e: 'Getting married'
};

const rebirths = cold(' -a--b---c--d-e|');
const firstMemories = cold('abcde', firstMemoryOf);
const expected = ' -abcabcdabcababcde';

const lifeStream = rebirths.pipe(
switchMap(() => firstMemories)
);

expectObservable(lifeStream).toBe(expected, firstMemoryOf);
});
});
});

describe('concatMap', () => {
it('concatenates the observables, releasing all their values in order', () => {
testScheduler.run((helpers) => {
const { cold, expectObservable } = helpers;

const catUpdates = {
a: 'Meowing',
b: 'Purring',
c: 'Biting',
d: 'Looking unimpressed',
e: 'Pretending to want to go out',
};

const catWatchReportNotifications = cold('-a-b---c|');
const tiddlesUpdates = cold('acccb|', catUpdates);
const dumplingUpdates = cold('---ddd|', catUpdates);
const felixUpdates = cold('-----dbace|', catUpdates);
const expected = '-acccb---ddd-----dbace|';

const catUpdater = getCatUpdate();

const catObserver = catWatchReportNotifications.pipe(
concatMap(() => catUpdater.next().value!)
);

expectObservable(catObserver).toBe(expected, catUpdates);

function* getCatUpdate() {
yield tiddlesUpdates;
yield dumplingUpdates;
yield felixUpdates;
}
});
});

describe('mergeMap', () => {
it('merges two observables into one', () => {
testScheduler.run((helpers) => {
const { cold, hot, expectObservable } = helpers;

const motionScan = cold( 's---s');
const legMovements = cold('ll-----l');
const armMovements = hot(' --a-aa');
const expected = 'll---a-l';

const motionDetector = getMotion();

const bodyMovements = motionScan.pipe(
mergeMap(() => {
return motionDetector.next().value!;
}
));

function* getMotion() {
yield legMovements;
yield armMovements;
}

expectObservable(bodyMovements).toBe(expected);
});
});
});

describe('exhaustMap', () => {
it('waits for the previous Observable to complete before outputting values from the new one', () => {
testScheduler.run((helpers) => {
const { cold, hot, expectObservable } = helpers;

const values = {
a: 'salt',
b: 'carrot',
c: 'coriander',
d: 'beef',
e: 'stock',
};

const chef = cold(' cc------c');
const soupRecipe = cold('a-c-b-e|', values);
const beefRecipe = hot('-b---------d-a-b-c', values);
const expected = ' a-c-b-e----d-a-b-c';

const recipeStorage = getRecipe();

const chefActions = chef.pipe(
exhaustMap(() => {
return recipeStorage.next().value!;
})
);

function* getRecipe() {
yield soupRecipe;
yield beefRecipe;
}

expectObservable(chefActions).toBe(expected, values);
});
});
});
});
});
12 changes: 12 additions & 0 deletions src/app/tutorials/mapsandmarbles-5/mapsandmarbles-5.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Component } from '@angular/core';

@Component({
selector: 'app-mapsandmarbles-5',
standalone: true,
imports: [],
templateUrl: './mapsandmarbles-5.component.html',
styleUrl: './mapsandmarbles-5.component.scss'
})
export class MapsandMarbles5Component {

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<h1>The Observable Itself</h1>
<div>
<button (click)="tryToConnectWithJohnCallBack()">Connect with John Callback</button>
<button (click)="tryToConnectWithJohnPromise()">Connect with John Promise</button>
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": [
"jasmine"
"jasmine",
"node"
]
},
"include": [
Expand Down

0 comments on commit e588dc2

Please sign in to comment.