-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy paththird-party.ts
111 lines (100 loc) · 3.4 KB
/
third-party.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// make sure you import mocha-config before @angular/core
import {assert} from "./test-config";
import {Component, ComponentRef, Directive, TemplateRef, ViewContainerRef} from "@angular/core";
import {View} from "tns-core-modules/ui/core/view";
import {Label} from "tns-core-modules/ui/label";
import {nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender} from "nativescript-angular/testing";
// >> third-party-simple-view-registration
import {registerElement} from "nativescript-angular/element-registry";
registerElement("third-party-view", () => require("./third-party-view").SimpleTag);
// << third-party-simple-view-registration
// >> third-party-simple-view-container
@Component({
selector: "simple-view-container",
template: `
<third-party-view prop1="value1"></third-party-view>
`
})
export class SimpleViewContainer {
}
// << third-party-simple-view-container
//
// >> third-party-document-form-component
@Component({
selector: "document-form",
template: ""
})
export class DocumentFormComponent {
// >> (hide)
public static titleLabel: Label;
// << (hide)
constructor() {
}
public setTitleView(view: View) {
// pass view parameter to native element...
// >> (hide)
DocumentFormComponent.titleLabel = <Label>view;
// << (hide)
}
}
// << third-party-document-form-component
// >> third-party-template-directive
@Directive({
selector: "[documentTitle]"
})
export class DocumentTitleDirective {
public static titleLabel: Label;
constructor(
private ownerForm: DocumentFormComponent,
private viewContainer: ViewContainerRef,
private template: TemplateRef<any>
) {
}
ngOnInit() {
const viewRef = this.viewContainer.createEmbeddedView(this.template);
// filter out whitespace nodes
const titleViews = viewRef.rootNodes.filter((node) =>
node && node.nodeName !== "#text");
if (titleViews.length > 0) {
const titleView = titleViews[0];
this.ownerForm.setTitleView(titleView);
}
}
}
// << third-party-template-directive
// >> third-party-document-form-container
@Component({
selector: "document-form-container",
template: `
<document-form src="document1.pdf">
<Label *documentTitle text="Document1"></Label>
</document-form>
`
})
export class DocumentFormContainer {
}
// << third-party-document-form-container
describe("Third party component snippets", () => {
before(() => {
registerElement("document-form", () => require("tns-core-modules/ui/layouts/stack-layout").StackLayout);
});
beforeEach(nsTestBedBeforeEach([
DocumentFormContainer,
DocumentFormComponent,
SimpleViewContainer,
DocumentTitleDirective
]));
afterEach(nsTestBedAfterEach());
it("instantiates SimpleView from markup", () => {
return nsTestBedRender(SimpleViewContainer).then((fixture) => {
const componentRef: ComponentRef<SimpleViewContainer> = fixture.componentRef;
const componentInstance = componentRef.instance;
assert.instanceOf(componentInstance, SimpleViewContainer);
});
});
it("renders DocumentForm with title template", () => {
return nsTestBedRender(DocumentFormContainer).then(() => {
assert.equal("Document1", DocumentFormComponent.titleLabel.text);
});
});
});