Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 92 additions & 24 deletions libs/template/spec/core/render-aware/render-aware_creator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { OnDestroy } from '@angular/core';
import { createRenderAware, RenderAware } from '../../../src/lib/core';
import { concat, EMPTY, NEVER, NextObserver, Observer, of, Unsubscribable } from 'rxjs';
import {
concat,
EMPTY,
NEVER,
NextObserver,
Observable,
Observer,
of,
Subject,
throwError,
Unsubscribable
} from 'rxjs';
import { startWith, tap } from 'rxjs/operators';
import { createRenderAware, RenderAware, StrategySelection } from '../../../src/lib/core';
import { DEFAULT_STRATEGY_NAME } from '../../../src/lib/render-strategies/strategies/strategies-map';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';
import createSpy = jasmine.createSpy;


// TODO: Add Angular decorator.
class CdAwareImplementation<U> implements OnDestroy {
Expand All @@ -22,20 +37,13 @@ class CdAwareImplementation<U> implements OnDestroy {
complete: () => (this.completed = true)
};

constructor() {
constructor(strategySelection: StrategySelection) {
this.cdAware = createRenderAware<U>({
strategies: {
[DEFAULT_STRATEGY_NAME]: {
name: DEFAULT_STRATEGY_NAME,
detectChanges: () => {},
scheduleCD: () => new AbortController(),
rxScheduleCD: (o) => o
}
},
strategies: strategySelection,
updateObserver: this.updateObserver,
resetObserver: this.resetObserver
});
this.cdAware.nextStrategy('local');
this.cdAware.nextStrategy(DEFAULT_STRATEGY_NAME);
this.subscription = this.cdAware.subscribe();
}

Expand All @@ -45,8 +53,23 @@ class CdAwareImplementation<U> implements OnDestroy {
}

let cdAwareImplementation: CdAwareImplementation<any>;
let strategies: StrategySelection;
const setupCdAwareImplementation = () => {
cdAwareImplementation = new CdAwareImplementation();
strategies = {
[DEFAULT_STRATEGY_NAME]: {
name: DEFAULT_STRATEGY_NAME,
detectChanges: createSpy('detectChanges'),
scheduleCD: () => new AbortController(),
rxScheduleCD: createSpy('rxScheduleCD').and.callFake(o => o)
},
testStrat: {
name: 'testStrat',
detectChanges: createSpy('detectChanges'),
scheduleCD: () => new AbortController(),
rxScheduleCD: createSpy('rxScheduleCD').and.callFake(o => o)
}
};
cdAwareImplementation = new CdAwareImplementation(strategies);
cdAwareImplementation.renderedValue = undefined;
cdAwareImplementation.error = undefined;
cdAwareImplementation.completed = false;
Expand Down Expand Up @@ -98,21 +121,26 @@ describe('CdAware', () => {
cdAwareImplementation.cdAware.nextPotentialObservable(NEVER);
expect(cdAwareImplementation.renderedValue).toBe(undefined);
});
// Also: 'should keep last emitted value in the view until a new observable NEVER was passed (as no value ever was emitted from new observable)'
// Also: 'should keep last emitted value in the view until a new observable NEVER was passed (as no value ever was
// emitted from new observable)'
it('should render emitted value from passed observable without changing it', () => {
cdAwareImplementation.cdAware.nextPotentialObservable(of(42));
expect(cdAwareImplementation.renderedValue).toBe(42);
});

it('should render undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)', () => {
cdAwareImplementation.cdAware.nextPotentialObservable(of(42));
expect(cdAwareImplementation.renderedValue).toBe(42);
cdAwareImplementation.cdAware.nextPotentialObservable(NEVER);
expect(cdAwareImplementation.renderedValue).toBe(undefined);
});
it(
'should render undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)',
() => {
cdAwareImplementation.cdAware.nextPotentialObservable(of(42));
expect(cdAwareImplementation.renderedValue).toBe(42);
cdAwareImplementation.cdAware.nextPotentialObservable(NEVER);
expect(cdAwareImplementation.renderedValue).toBe(undefined);
}
);
});

describe('observable context', () => {
// TODO: naming? What's this test for? I don't understand
it('next handling running observable', () => {
cdAwareImplementation.cdAware.nextPotentialObservable(
concat(of(42), NEVER)
Expand All @@ -131,12 +159,12 @@ describe('CdAware', () => {

it('error handling', () => {
expect(cdAwareImplementation.renderedValue).toBe(undefined);
cdAwareImplementation.cdAware.subscribe({
error: (e: Error) => expect(e).toBeDefined()
});
const error = new Error();
cdAwareImplementation.cdAware.nextPotentialObservable(throwError(error));
expect(cdAwareImplementation.renderedValue).toBe(undefined);
// @TODO use this line
// @TODO use this line: why?
// expect(cdAwareImplementation.error).toBe(ArgumentNotObservableError);
expect(cdAwareImplementation.error).toBe(error);
expect(cdAwareImplementation.completed).toBe(false);
});

Expand All @@ -147,4 +175,44 @@ describe('CdAware', () => {
expect(cdAwareImplementation.completed).toBe(true);
});
});

describe('strategy handling', () => {
it('should handle strategy switching', () => {
const values = new Subject();
const value$ = values.pipe(startWith(1), tap(console.log));
cdAwareImplementation.cdAware.nextPotentialObservable(
value$
);
expect(cdAwareImplementation.renderedValue).toBe(1);
expect(strategies[DEFAULT_STRATEGY_NAME].rxScheduleCD).toHaveBeenCalled();
cdAwareImplementation.cdAware.nextStrategy('testStrat');
values.next(2);
expect(strategies.testStrat.rxScheduleCD).toHaveBeenCalled();
expect(cdAwareImplementation.renderedValue).toBe(2);
expect(cdAwareImplementation.error).toBe(undefined);
expect(cdAwareImplementation.completed).toBe(false);
});

it('should handle subscriptions with ongoing observables', () => {
const values = new Subject();
let subscribers = 0;
const v$ = new Observable(subscriber => {
subscribers++;
values.pipe(startWith(1)).subscribe(innerValue => {
subscriber.next(innerValue);
});
});
cdAwareImplementation.cdAware.nextPotentialObservable(v$);
expect(cdAwareImplementation.renderedValue).toBe(1);
values.next(2);
expect(cdAwareImplementation.renderedValue).toBe(2);
cdAwareImplementation.cdAware.nextStrategy('testStrat');
expect(cdAwareImplementation.renderedValue).toBe(2);
values.next(3);
expect(cdAwareImplementation.renderedValue).toBe(3);
expect(cdAwareImplementation.error).toBe(undefined);
expect(cdAwareImplementation.completed).toBe(false);
expect(subscribers).toBe(1);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createPropertiesWeakMap } from '../../../src/lib/core/utils';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

const propertyNameString = 'flag';
Expand Down
1 change: 1 addition & 0 deletions libs/template/spec/core/utils/get-global-this.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getGlobalThis } from '../../../src/lib/core';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';


Expand Down
1 change: 1 addition & 0 deletions libs/template/spec/core/utils/view-engine-checks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getGlobalThis, isViewEngineIvy } from '../../../src/lib/core';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';


Expand Down
1 change: 1 addition & 0 deletions libs/template/spec/core/utils/zone-check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApplicationRef, Component, NgModule, NgZone } from '@angular/core';
import { getTestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { isNgZone, isNoopNgZone } from '../../../src/lib/core/utils';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';


Expand Down
3 changes: 1 addition & 2 deletions libs/template/spec/fixtures/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import createSpy = jasmine.createSpy;
import { ChangeDetectorRef } from '@angular/core';

export class MockChangeDetectorRef {
markForCheck = createSpy('markForCheck');
detectChanges = createSpy('detectChanges');
}

export const mockPromise = {
then: () => {}
then: () => {},
};
1 change: 1 addition & 0 deletions libs/template/spec/let/let.directive.complete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EMPTY, Observable, of } from 'rxjs';
import { async, TestBed } from '@angular/core/testing';
import { LetDirective } from '../../src/lib/let';
import { MockChangeDetectorRef } from '../fixtures';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

@Component({
Expand Down
2 changes: 2 additions & 0 deletions libs/template/spec/let/let.directive.error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Observable, of, throwError } from 'rxjs';
import { async, TestBed } from '@angular/core/testing';
import { LetDirective } from '../../src/lib/let';
import { MockChangeDetectorRef } from '../fixtures';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';


@Component({
template: `
<ng-container *rxLet="value$; $error as error">{{ error }}</ng-container>
Expand Down
1 change: 1 addition & 0 deletions libs/template/spec/let/let.directive.next.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { LetDirective } from '../../src/lib/let';
import { take } from 'rxjs/operators';
import { MockChangeDetectorRef } from '../fixtures';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

@Component({
Expand Down
1 change: 1 addition & 0 deletions libs/template/spec/let/let.directive.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Observable, of } from 'rxjs';
import { TestBed } from '@angular/core/testing';
import { LetDirective } from '../../src/lib/let';
import { MockChangeDetectorRef } from '../fixtures';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core
import { LetDirective } from '@rx-angular/template';
import { EMPTY, interval, Observable, of, Subject, NEVER, throwError } from 'rxjs';
import { take } from 'rxjs/operators';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LetDirective } from '@rx-angular/template';
import { EMPTY, Observable, of } from 'rxjs';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LetDirective } from '@rx-angular/template';
import { Observable, of, Subject } from 'rxjs';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LetDirective } from '@rx-angular/template';
import { Observable, of, Subject } from 'rxjs';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

@Component({
Expand Down
2 changes: 1 addition & 1 deletion libs/template/spec/push/push.pipe.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PushPipe } from '../../src/lib/push';
import { async, TestBed } from '@angular/core/testing';
import { ChangeDetectorRef, Component } from '@angular/core';
import { getGlobalThis } from '../../src/lib/core/utils';
import { EMPTY, NEVER, Observable, of } from 'rxjs';
import { MockChangeDetectorRef } from '../fixtures';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

let pushPipe: any;
Expand Down
1 change: 1 addition & 0 deletions libs/template/spec/push/push.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PushPipe } from '../../src/lib/push';
import { TestBed } from '@angular/core/testing';
import { ChangeDetectorRef, Component } from '@angular/core';
import { EMPTY, NEVER, Observable, of } from 'rxjs';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

function wrapWithSpace(str: string): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from "@test-helpers";


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { staticCoalesce } from '../../../src/lib/render-strategies/static';
import { priorityTickMap, SchedulingPriority } from '@rx-angular/template';
import { from } from 'rxjs';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

/** @test {coalesceWith} */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { coalesceAndSchedule } from '../../../src/lib/render-strategies/static';
import { SchedulingPriority } from '../../../src/lib/render-strategies/rxjs/scheduling/interfaces';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';

/** @test {coalesceWith} */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { staticSchedule } from '../../../src/lib/render-strategies/static';
import { SchedulingPriority } from '../../../src/lib/render-strategies/rxjs/scheduling';
// tslint:disable-next-line:nx-enforce-module-boundaries
import { mockConsole } from '@test-helpers';


Expand Down
Loading