Skip to content

Commit fc841bd

Browse files
committed
fix(): fix tests
1 parent 12d908e commit fc841bd

File tree

5 files changed

+40
-42
lines changed

5 files changed

+40
-42
lines changed

projects/transloco/src/lib/tests/transloco.mocks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { TRANSLOCO_PARSER, DefaultParser } from '../transloco.parser';
1+
import { DefaultParser, TRANSLOCO_PARSER } from '../transloco.parser';
22
import { TRANSLOCO_LOADER } from '../transloco.loader';
3-
import { TRANSLOCO_CONFIG, defaultConfig } from '../transloco.config';
3+
import { defaultConfig, TRANSLOCO_CONFIG } from '../transloco.config';
44
import { timer } from 'rxjs';
55
import { map } from 'rxjs/operators';
66
import { DefaultHandler, TRANSLOCO_MISSING_HANDLER } from '../transloco-missing-handler';

projects/transloco/src/lib/tests/transloco.pipe.spec.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { TranslocoService, TranslocoPipe, DefaultParser } from '../../public-api';
1+
import { DefaultParser, TranslocoPipe, TranslocoService } from '../../public-api';
22
import { Mock } from 'ts-mocks';
33
import { ChangeDetectorRef } from '@angular/core';
44
import { load, runLoader } from './transloco.mocks';
5-
import {fakeAsync, tick} from '@angular/core/testing';
5+
import { fakeAsync } from '@angular/core/testing';
66
import { DefaultHandler } from '../transloco-missing-handler';
7-
import Spy = jasmine.Spy;
8-
import {of} from "rxjs";
9-
import createSpy = jasmine.createSpy;
7+
import { of } from "rxjs";
108

119
describe('TranslocoPipe', () => {
1210
let translateServiceMock;
@@ -17,7 +15,10 @@ describe('TranslocoPipe', () => {
1715
translateServiceMock = new Mock<TranslocoService>(
1816
new TranslocoService(load, new DefaultParser(), new DefaultHandler(), {})
1917
).Object;
20-
cdrMock = new Mock<ChangeDetectorRef>({ markForCheck: () => {} }).Object;
18+
cdrMock = new Mock<ChangeDetectorRef>({
19+
markForCheck: () => {
20+
}
21+
}).Object;
2122
pipe = new TranslocoPipe(translateServiceMock, {}, cdrMock);
2223
spyOn(pipe, 'updateValue').and.callThrough();
2324
});
@@ -45,13 +46,11 @@ describe('TranslocoPipe', () => {
4546

4647
describe('transform', () => {
4748
it('should unsubscribe after one emit when not in runtime mode', fakeAsync(() => {
48-
pipe = new TranslocoPipe(translateServiceMock, {runtime: false}, cdrMock);
49+
pipe = new TranslocoPipe(translateServiceMock, { runtime: false }, cdrMock);
50+
const spy = spyOn(pipe as any, 'takeOne').and.callThrough();
4951
pipe.transform('home');
50-
const spy = createSpy().and.callThrough();
51-
pipe.subscription.unsubscribe = spy;
5252
runLoader();
53-
expect(spy).toHaveBeenCalled();
54-
expect(pipe.subscription).toBe(null);
53+
expect(spy).toHaveBeenCalledTimes(1);
5554
}));
5655

5756
it('should return the key when the key is falsy', () => {
@@ -75,23 +74,21 @@ describe('TranslocoPipe', () => {
7574
it('should return the value from the cache', fakeAsync(() => {
7675
pipe.transform('home');
7776
runLoader();
78-
expect(pipe.updateValue).toHaveBeenCalled();
79-
(pipe.updateValue as Spy).calls.reset();
77+
expect(pipe.updateValue).toHaveBeenCalledTimes(1);
8078
pipe.transform('home');
81-
expect(pipe.updateValue).not.toHaveBeenCalled();
79+
expect(pipe.updateValue).toHaveBeenCalledTimes(1);
8280
pipe.transform('a.b.c');
83-
expect(pipe.updateValue).toHaveBeenCalled();
81+
expect(pipe.updateValue).toHaveBeenCalledTimes(2);
8482
}));
8583

86-
fit('should return the value from the cache with params', fakeAsync(() => {
84+
it('should return the value from the cache with params', fakeAsync(() => {
8785
pipe.transform('alert', { value: 'value' });
8886
runLoader();
89-
expect(pipe.updateValue).toHaveBeenCalled();
90-
(pipe.updateValue as Spy).calls.reset();
87+
expect(pipe.updateValue).toHaveBeenCalledTimes(1);
9188
pipe.transform('alert', { value: 'value' });
92-
expect(pipe.updateValue).not.toHaveBeenCalled();
89+
expect(pipe.updateValue).toHaveBeenCalledTimes(1);
9390
pipe.transform('alert', { value: 'bla' });
94-
expect(pipe.updateValue).toHaveBeenCalled();
91+
expect(pipe.updateValue).toHaveBeenCalledTimes(2);
9592
}));
9693
});
9794

projects/transloco/src/lib/transloco.directive.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
TemplateRef,
1313
ViewContainerRef
1414
} from '@angular/core';
15-
import { switchMap } from 'rxjs/operators';
15+
import { switchMap, take } from 'rxjs/operators';
1616
import { Subscription } from 'rxjs';
1717
import { TranslocoService } from './transloco.service';
1818
import { HashMap } from './types';
@@ -37,22 +37,21 @@ export class TranslocoDirective implements OnInit, OnDestroy, OnChanges {
3737
private vcr: ViewContainerRef,
3838
private cdr: ChangeDetectorRef,
3939
private host: ElementRef
40-
) {}
40+
) {
41+
}
4142

4243
ngOnInit() {
4344
this.hasLoadingTpl() && this.vcr.createEmbeddedView(this.loadingTpl);
4445

4546
const { runtime } = this.translocoService.config;
4647
this.subscription = this.translocoService.lang$
47-
.pipe(switchMap(lang => this.translocoService._load(this.getScope() ? `${lang}-${this.getScope()}` : lang)))
48+
.pipe(
49+
switchMap(lang => this.translocoService._load(this.getScope() ? `${lang}-${this.getScope()}` : lang)),
50+
runtime ? source => source : take(1)
51+
)
4852
.subscribe(data => {
4953
this.tpl === null ? this.simpleStrategy() : this.structuralStrategy(data);
5054
this.cdr.markForCheck();
51-
52-
if (!runtime) {
53-
this.subscription.unsubscribe();
54-
this.subscription = null;
55-
}
5655
});
5756
}
5857

@@ -68,7 +67,7 @@ export class TranslocoDirective implements OnInit, OnDestroy, OnChanges {
6867
}
6968

7069
private structuralStrategy(data) {
71-
if (this.view) {
70+
if( this.view ) {
7271
this.view.context['$implicit'] = data;
7372
} else {
7473
this.hasLoadingTpl() && this.vcr.clear();

projects/transloco/src/lib/transloco.pipe.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ChangeDetectorRef, Inject, OnDestroy, Pipe, PipeTransform } from '@angu
22
import { TranslocoService } from './transloco.service';
33
import { HashMap } from './types';
44
import { defaultConfig, TRANSLOCO_CONFIG, TranslocoConfig } from './transloco.config';
5-
import { switchMap } from 'rxjs/operators';
5+
import { finalize, switchMap, take, tap } from 'rxjs/operators';
66
import { Subscription } from 'rxjs';
77

88
@Pipe({
@@ -32,11 +32,11 @@ export class TranslocoPipe implements PipeTransform, OnDestroy {
3232
}
3333

3434
transform(key: string, params: HashMap = {}): string {
35-
if (!key) {
35+
if( !key ) {
3636
return key;
3737
}
3838

39-
if (key === this.lastKey && JSON.stringify(params) === JSON.stringify(this.lastParams)) {
39+
if( key === this.lastKey && JSON.stringify(params) === JSON.stringify(this.lastParams) ) {
4040
return this.value;
4141
}
4242

@@ -48,14 +48,12 @@ export class TranslocoPipe implements PipeTransform, OnDestroy {
4848
this.subscription && this.subscription.unsubscribe();
4949

5050
this.subscription = this.translocoService.lang$
51-
.pipe(switchMap(lang => this.translocoService._load(lang)))
52-
.subscribe(data => {
51+
.pipe(
52+
switchMap(lang => this.translocoService._load(lang)),
53+
this.runtime ? source => source : this.takeOne()
54+
)
55+
.subscribe(() => {
5356
this.updateValue(key, params);
54-
55-
if (!this.runtime) {
56-
this.subscription.unsubscribe();
57-
this.subscription = null;
58-
}
5957
});
6058

6159
return this.value;
@@ -64,4 +62,8 @@ export class TranslocoPipe implements PipeTransform, OnDestroy {
6462
ngOnDestroy() {
6563
this.subscription && this.subscription.unsubscribe();
6664
}
65+
66+
private takeOne() {
67+
return take(1);
68+
}
6769
}

projects/transloco/src/lib/transloco.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class TranslocoService {
9898
this.langs.set(lang, value);
9999
this.translationLoaded.next({ lang });
100100
}),
101-
shareReplay({ refCount: true, bufferSize: 1 })
101+
shareReplay({ refCount: false, bufferSize: 1 })
102102
);
103103
this.cache.set(lang, load$);
104104
}

0 commit comments

Comments
 (0)