Skip to content

Commit 446e298

Browse files
committed
feat(core): added new pipe CallPipe to call component methods are pure pipes
Example: text | call: 'method' text | call: method text | call: method: 'additional': 'arguments'
1 parent de63285 commit 446e298

7 files changed

Lines changed: 113 additions & 32 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngxf",
3-
"version": "6.0.5",
3+
"version": "6.0.6",
44
"scripts": {
55
"ng": "ng",
66
"start": "ng serve",

projects/platform/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ngxf/platform",
3-
"version": "6.0.5",
3+
"version": "6.0.6",
44
"description": "NGXF - Non-State Management for Angular",
55
"keywords": [
66
"non-ngxs",

projects/platform/src/lib/directives/index.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
import { AsyncDirective } from './async.directive';
2-
import { ComposeDirective } from './compose.directive';
3-
import { CookiesDirective } from './cookies.directive';
4-
import { HttpDirective } from './http.directive';
5-
import { LazyDirective } from './lazy.directive';
6-
import { InitDirective } from './init.directive';
7-
import { NestDirective } from './nest.directive';
8-
import { RenamePropDirective } from './rename-prop.directive';
9-
import { ReturnDirective } from './return.directive';
10-
import { RouteDirective } from './route.directive';
11-
import { SetPropsDirective } from './set-props.directive';
12-
import { TimeoutDirective } from './timeout.directive';
13-
14-
export const DIRECTIVES = [
15-
AsyncDirective,
16-
ComposeDirective,
17-
CookiesDirective,
18-
HttpDirective,
19-
LazyDirective,
20-
InitDirective,
21-
NestDirective,
22-
RenamePropDirective,
23-
ReturnDirective,
24-
RouteDirective,
25-
SetPropsDirective,
26-
TimeoutDirective
27-
];
28-
291
export * from './async.directive';
302
export * from './compose.directive';
313
export * from './cookies.directive';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ChangeDetectorRef, EmbeddedViewRef, Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({ name: 'call' })
4+
export class CallPipe implements PipeTransform {
5+
context: any;
6+
7+
constructor(cd: ChangeDetectorRef) {
8+
this.context = (cd as EmbeddedViewRef<any>).context;
9+
}
10+
11+
transform(param: any, fn: string | Function, ...params: any[]) {
12+
if (typeof fn === 'string') {
13+
fn = this.context[fn];
14+
}
15+
16+
if (typeof fn !== 'function') {
17+
fn = () => { };
18+
}
19+
20+
return fn.apply(this.context, [param, ...params]);
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './call.pipe';

projects/platform/src/lib/platform.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ANALYZE_FOR_ENTRY_COMPONENTS, ModuleWithProviders, NgModule, Type } from '@angular/core';
22
import { AsyncDirective, ComposeDirective, CookiesDirective, HttpDirective, InitDirective, LAZY_COMPONENT_TOKEN, LazyDirective, NestDirective, RenamePropDirective, ReturnDirective, RouteDirective, SetPropsDirective, TimeoutDirective } from './directives';
3+
import { CallPipe } from './pipes';
34

45
const DIRECTIVES = [
56
AsyncDirective,
@@ -16,10 +17,14 @@ const DIRECTIVES = [
1617
TimeoutDirective
1718
];
1819

20+
const PIPES = [
21+
CallPipe
22+
];
23+
1924
@NgModule({
2025
imports: [],
21-
declarations: [ DIRECTIVES ],
22-
exports: [ DIRECTIVES ]
26+
declarations: [ DIRECTIVES, PIPES ],
27+
exports: [ DIRECTIVES, PIPES ]
2328
})
2429
export class NgxfModule {
2530
static forLazy(component: Type<any>): ModuleWithProviders {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { Component } from '@angular/core';
2+
import { fakeAsync } from '@angular/core/testing';
3+
import { createHostComponentFactory, SpectatorWithHost } from '@netbasal/spectator';
4+
import { CallPipe } from '../../lib/pipes';
5+
6+
const TEXT = 'NGX Features Awesome';
7+
const TEXT2 = 'Really!';
8+
9+
@Component({ selector: 'host', template: '' })
10+
class Host {
11+
text: string = TEXT;
12+
method(value: string) {
13+
return this.transform(value);
14+
}
15+
16+
transform(value: string) {
17+
return value && value.toUpperCase();
18+
}
19+
}
20+
21+
describe('CallPipe', () => {
22+
let host: SpectatorWithHost<Host, Host>;
23+
const create = createHostComponentFactory({
24+
host: Host,
25+
component: Host,
26+
declarations: [ CallPipe ]
27+
});
28+
29+
it('should call component method at once', fakeAsync(() => {
30+
host = create(`{{ text | call: 'method' }}`, false);
31+
spyOn(host.hostComponent, 'method').and.callThrough();
32+
33+
host.detectChanges();
34+
expect(host.hostElement).toHaveText(TEXT.toUpperCase());
35+
36+
host.detectChanges();
37+
expect(host.hostComponent.method).toHaveBeenCalledTimes(1);
38+
}));
39+
40+
it('should call component method after change text', fakeAsync(() => {
41+
host = create(`{{ text | call: 'method' }}`, false);
42+
spyOn(host.hostComponent, 'method').and.callThrough();
43+
44+
host.detectChanges();
45+
expect(host.hostElement).toHaveText(TEXT.toUpperCase());
46+
47+
host.detectChanges();
48+
expect(host.hostComponent.method).toHaveBeenCalledTimes(1);
49+
50+
host.setHostInput({ text: TEXT2 });
51+
expect(host.hostElement).toHaveText(TEXT2.toUpperCase());
52+
53+
host.detectChanges();
54+
expect(host.hostComponent.method).toHaveBeenCalledTimes(2);
55+
}));
56+
57+
it('should call method by string', fakeAsync(() => {
58+
host = create(`{{ text | call: 'method' }}`, false);
59+
spyOn(host.hostComponent, 'method').and.callThrough();
60+
61+
host.detectChanges();
62+
expect(host.hostComponent.method).toHaveBeenCalledTimes(1);
63+
}));
64+
65+
it('should call method by function', fakeAsync(() => {
66+
host = create(`{{ text | call: method }}`, false);
67+
spyOn(host.hostComponent, 'method').and.callThrough();
68+
69+
host.detectChanges();
70+
expect(host.hostComponent.method).toHaveBeenCalledTimes(1);
71+
}));
72+
73+
it('should call method with extra arguments', fakeAsync(() => {
74+
host = create(`{{ text | call: method: 'foo': 'bar' }}`, false);
75+
spyOn(host.hostComponent, 'method').and.callThrough();
76+
77+
host.detectChanges();
78+
expect(host.hostComponent.method).toHaveBeenCalledWith(TEXT, 'foo', 'bar');
79+
}));
80+
81+
});

0 commit comments

Comments
 (0)