2018-02-16 10:23:27 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2020-02-15 06:10:56 -05:00
|
|
|
import {Injectable as _Injectable, Pipe as _Pipe, PipeTransform, WrappedValue, ɵɵdefineInjectable, ɵɵdefinePipe, ɵɵgetInheritedFactory, ɵɵinject} from '@angular/core';
|
2018-02-16 10:23:27 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
|
|
|
|
2019-09-06 17:43:16 -04:00
|
|
|
import {ɵɵtext, ɵɵtextInterpolate1} from '../../src/render3/instructions/all';
|
2019-05-17 21:49:21 -04:00
|
|
|
import {ɵɵpipe, ɵɵpipeBind1} from '../../src/render3/pipe';
|
2018-02-16 10:23:27 -05:00
|
|
|
|
2019-05-10 10:59:06 -04:00
|
|
|
import {TemplateFixture} from './render_util';
|
2018-02-16 10:23:27 -05:00
|
|
|
|
2018-10-30 20:03:01 -04:00
|
|
|
const Pipe: typeof _Pipe = function(...args: any[]): any {
|
|
|
|
// In test we use @Pipe for documentation only so it's safe to mock out the implementation.
|
|
|
|
return () => undefined;
|
|
|
|
} as any;
|
2018-02-16 10:23:27 -05:00
|
|
|
|
2020-02-15 06:10:56 -05:00
|
|
|
const Injectable: typeof _Injectable = function(...args: any[]): any {
|
|
|
|
// In test we use @Injectable for documentation only so it's safe to mock out the implementation.
|
|
|
|
return () => undefined;
|
|
|
|
} as any;
|
|
|
|
|
2018-02-16 10:23:27 -05:00
|
|
|
|
|
|
|
describe('pipe', () => {
|
2020-02-15 06:10:56 -05:00
|
|
|
// TODO: hasn't been moved over into acceptance, because the `WrappedValue` tests need to
|
|
|
|
// use an impure pipe which always throws "changed after checked errors" with `TestBed`
|
|
|
|
// both in Ivy and ViewEngine.
|
2018-12-03 05:40:07 -05:00
|
|
|
describe('WrappedValue', () => {
|
|
|
|
@Pipe({name: 'wrappingPipe'})
|
|
|
|
class WrappingPipe implements PipeTransform {
|
2020-04-13 19:40:21 -04:00
|
|
|
transform(value: any) {
|
|
|
|
return new WrappedValue('Bar');
|
|
|
|
}
|
2018-12-03 05:40:07 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
static ɵfac = function WrappingPipe_Factory() {
|
|
|
|
return new WrappingPipe();
|
|
|
|
};
|
2019-10-11 22:19:59 -04:00
|
|
|
static ɵpipe = ɵɵdefinePipe({name: 'wrappingPipe', type: WrappingPipe, pure: false});
|
2018-12-03 05:40:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function createTemplate() {
|
2019-05-17 21:49:21 -04:00
|
|
|
ɵɵtext(0);
|
|
|
|
ɵɵpipe(1, 'wrappingPipe');
|
2018-12-03 05:40:07 -05:00
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
function updateTemplate() {
|
|
|
|
ɵɵtextInterpolate1('', ɵɵpipeBind1(1, 1, null), '');
|
|
|
|
}
|
2018-12-03 05:40:07 -05:00
|
|
|
|
|
|
|
it('should unwrap', () => {
|
|
|
|
const fixture =
|
|
|
|
new TemplateFixture(createTemplate, updateTemplate, 2, 3, undefined, [WrappingPipe]);
|
|
|
|
expect(fixture.html).toEqual('Bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should force change detection', () => {
|
|
|
|
const fixture =
|
|
|
|
new TemplateFixture(createTemplate, updateTemplate, 2, 3, undefined, [WrappingPipe]);
|
|
|
|
expect(fixture.html).toEqual('Bar');
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
fixture.hostElement.childNodes[0]!.textContent = 'Foo';
|
2018-12-03 05:40:07 -05:00
|
|
|
expect(fixture.html).toEqual('Foo');
|
|
|
|
|
|
|
|
fixture.update();
|
|
|
|
expect(fixture.html).toEqual('Bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-15 06:10:56 -05:00
|
|
|
// This test isn't in `acceptance`, because we can't capture the same behavior that we want
|
|
|
|
// when going through `TestBed`. Here we're testing the behavior of AOT-compiled code which
|
|
|
|
// differs from the JIT code in `TestBed`, because it includes a `ɵɵgetInheritedFactory` call
|
|
|
|
// when the pipe is using inheritance.
|
|
|
|
it('should be able to use DI in a Pipe that extends an Injectable', () => {
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
class SayHelloService {
|
2020-04-13 19:40:21 -04:00
|
|
|
getHello() {
|
|
|
|
return 'Hello there';
|
|
|
|
}
|
2020-02-15 06:10:56 -05:00
|
|
|
static ɵfac = () => new SayHelloService();
|
|
|
|
static ɵprov = ɵɵdefineInjectable(
|
|
|
|
{token: SayHelloService, factory: SayHelloService.ɵfac, providedIn: 'root'});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
class ParentPipe {
|
|
|
|
constructor(protected sayHelloService: SayHelloService) {}
|
2020-04-13 19:40:21 -04:00
|
|
|
static ɵfac = (t?: any) => new(t || ParentPipe)(ɵɵinject(SayHelloService));
|
2020-02-15 06:10:56 -05:00
|
|
|
static ɵprov = ɵɵdefineInjectable({token: ParentPipe, factory: ParentPipe.ɵfac});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Pipe({name: 'sayHello', pure: true})
|
|
|
|
class SayHelloPipe extends ParentPipe implements PipeTransform {
|
2020-04-13 19:40:21 -04:00
|
|
|
transform() {
|
|
|
|
return this.sayHelloService.getHello();
|
|
|
|
}
|
2020-02-15 06:10:56 -05:00
|
|
|
static ɵfac = (t?: any) => ɵɵgetInheritedFactory(t || SayHelloPipe)(SayHelloPipe);
|
|
|
|
static ɵpipe = ɵɵdefinePipe({name: 'sayHello', type: SayHelloPipe, pure: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
const fixture = new TemplateFixture(
|
|
|
|
() => {
|
|
|
|
ɵɵtext(0);
|
|
|
|
ɵɵpipe(1, 'sayHello');
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
() => {
|
|
|
|
ɵɵtextInterpolate1('', ɵɵpipeBind1(1, 1, null), '');
|
|
|
|
},
|
|
|
|
2, 3, undefined, [SayHelloPipe]);
|
2020-02-15 06:10:56 -05:00
|
|
|
|
|
|
|
expect(fixture.html).toBe('Hello there');
|
|
|
|
});
|
2018-02-16 10:23:27 -05:00
|
|
|
});
|