Skip to content

Commit cd12846

Browse files
committed
Enhance example and add e2e test for example
1 parent 64f072d commit cd12846

File tree

10 files changed

+60
-41
lines changed

10 files changed

+60
-41
lines changed

external-scripts/example-script.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function TextChangerUtil(elementId) {
2+
this.elementID = elementId;
3+
}
4+
5+
TextChangerUtil.prototype.changeText = function(newText) {
6+
document.getElementById(this.elementID).innerText = newText;
7+
};

projects/ng-lazyload-script-example/e2e/src/app.e2e-spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import { AppPage } from './app.po';
2-
import { browser, logging } from 'protractor';
1+
import {AppPage} from './app.po';
2+
import {browser, ExpectedConditions, logging} from 'protractor';
33

4-
describe('workspace-project App', () => {
4+
describe('example app', () => {
55
let page: AppPage;
66

77
beforeEach(() => {
88
page = new AppPage();
99
});
1010

11-
it('should display welcome message', () => {
11+
it('should eventually change text', () => {
1212
page.navigateTo();
13-
expect(page.getTitleText()).toEqual('ng-lazyload-script-example app is running!');
13+
14+
expect(page.getTextBox().getText()).toEqual('A new text should appear here...');
15+
16+
page.clickButton();
17+
18+
browser.wait(
19+
ExpectedConditions.textToBePresentInElement(
20+
page.getTextBox(),
21+
'A new text (set by text changer util which was lazy loaded)'),
22+
10000);
1423
});
1524

1625
afterEach(async () => {
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { browser, by, element } from 'protractor';
1+
import { browser, by, element, WebElementPromise } from 'protractor';
22

33
export class AppPage {
44
navigateTo() {
55
return browser.get(browser.baseUrl) as Promise<any>;
66
}
77

8-
getTitleText() {
9-
return element(by.css('app-root .content span')).getText() as Promise<string>;
8+
getTextBox() {
9+
return element(by.id('text-box'));
10+
}
11+
12+
clickButton() {
13+
return element(by.id('button')).click();
1014
}
1115
}

projects/ng-lazyload-script-example/src/app/app.component.html

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ <h1>
22
NgLazyScript Example Application
33
</h1>
44

5-
<div id="red-box">
6-
This box should eventually get a red background.
7-
</div>
5+
<h2>
6+
Text box
7+
</h2>
8+
<p id="text-box">
9+
A new text should appear here...
10+
</p>
11+
12+
<h2>
13+
Lazy load script button
14+
</h2>
15+
<p>
16+
As soon as this button is clicked, an external script is loaded on the fly and the loaded
17+
library is used to change the text of the text box.
18+
</p>
19+
<button type="button" id="button" (click)="loadScript()">
20+
Load script and change text
21+
</button>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#text-box {
2+
border: 1px solid black;
3+
padding: 5px;
4+
}

projects/ng-lazyload-script-example/src/app/app.component.spec.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,4 @@ describe('AppComponent', () => {
1515
const app = fixture.debugElement.componentInstance;
1616
expect(app).toBeTruthy();
1717
});
18-
19-
it(`should have as title 'ng-lazyload-script-example'`, () => {
20-
const fixture = TestBed.createComponent(AppComponent);
21-
const app = fixture.debugElement.componentInstance;
22-
expect(app.title).toEqual('ng-lazyload-script-example');
23-
});
24-
25-
it('should render title', () => {
26-
const fixture = TestBed.createComponent(AppComponent);
27-
fixture.detectChanges();
28-
const compiled = fixture.debugElement.nativeElement;
29-
expect(compiled.querySelector('.content span').textContent).toContain('ng-lazyload-script-example app is running!');
30-
});
3118
});

projects/ng-lazyload-script-example/src/app/app.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ export class AppComponent implements OnInit {
1212
}
1313

1414
ngOnInit() {
15+
}
16+
17+
loadScript() {
1518
// Load the external script on the fly
16-
this.ngLazyScriptService.loadScript('http://ng-lazy-script.uapps.ch/example-script.js').subscribe(() => {
17-
const colorUtil = new ColorUtil('red-box');
18-
colorUtil.changeBackgroundColor('red');
19+
this.ngLazyScriptService.loadScript('http://ng-lazy-script.uapps.ch/text-changer-util.js').subscribe(() => {
20+
const textChangerUtil = new TextChangerUtil('text-box');
21+
textChangerUtil.changeText('A new text (set by text changer util which was lazy loaded)');
1922
});
2023
}
21-
2224
}
2325

24-

projects/ng-lazyload-script-example/src/typings/color-util.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare class TextChangerUtil {
2+
constructor(elementId: string);
3+
4+
changeText(newText: string);
5+
}

0 commit comments

Comments
 (0)