Skip to content

Commit f32c25a

Browse files
author
vakrilov
committed
Add tests
1 parent 36dba60 commit f32c25a

File tree

5 files changed

+82
-18
lines changed

5 files changed

+82
-18
lines changed

Diff for: nativescript-angular/file-system/ns-file-system.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from "@angular/core";
2-
import { knownFolders, Folder } from "tns-core-modules/file-system";
2+
import { knownFolders, Folder, File } from "tns-core-modules/file-system";
33

44
// Allows greater flexibility with `file-system` and Angular
55
// Also provides a way for `file-system` to be mocked for testing
@@ -9,4 +9,12 @@ export class NSFileSystem {
99
public currentApp(): Folder {
1010
return knownFolders.currentApp();
1111
}
12+
13+
public fileFromPath(path: string): File {
14+
return File.fromPath(path);
15+
}
16+
17+
public fileExists(path: string): boolean {
18+
return File.exists(path);
19+
}
1220
}

Diff for: nativescript-angular/platform.ts

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ if ((<any>global).___TS_UNUSED) {
3535
import "./dom-adapter";
3636

3737
import { NativeScriptElementSchemaRegistry } from "./schema-registry";
38+
import { NSFileSystem } from "./file-system/ns-file-system";
3839
import { FileSystemResourceLoader } from "./resource-loader";
3940

4041
export const NS_COMPILER_PROVIDERS = [
@@ -43,6 +44,7 @@ export const NS_COMPILER_PROVIDERS = [
4344
provide: COMPILER_OPTIONS,
4445
useValue: {
4546
providers: [
47+
NSFileSystem,
4648
{ provide: ResourceLoader, useClass: FileSystemResourceLoader },
4749
{ provide: ElementSchemaRegistry, useClass: NativeScriptElementSchemaRegistry },
4850
]

Diff for: nativescript-angular/resource-loader.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1+
import { Injectable } from "@angular/core";
12
import { ResourceLoader } from "@angular/compiler";
2-
import { File, knownFolders, path } from "tns-core-modules/file-system";
3+
import { path } from "tns-core-modules/file-system";
4+
5+
import { NSFileSystem } from "./file-system/ns-file-system";
36

47
const extensionsFallbacks = [
58
[".scss", ".css"],
69
[".sass", ".css"],
710
[".less", ".css"]
811
];
912

13+
@Injectable()
1014
export class FileSystemResourceLoader extends ResourceLoader {
15+
constructor(private fs: NSFileSystem) {
16+
super();
17+
}
18+
1119
get(url: string): Promise<string> {
1220
const resolvedPath = this.resolve(url);
1321

14-
const templateFile = File.fromPath(resolvedPath);
22+
const templateFile = this.fs.fileFromPath(resolvedPath);
1523

1624
return templateFile.readText();
1725
}
1826

19-
private handleAbsoluteUrls(url: string): string {
27+
resolveRelativeUrls(url: string): string {
2028
// Angular assembles absolute URLs and prefixes them with //
2129
if (url.indexOf("/") !== 0) {
2230
// Resolve relative URLs based on the app root.
23-
return path.join(knownFolders.currentApp().path, url);
31+
return path.join(this.fs.currentApp().path, url);
2432
} else {
2533
return url;
2634
}
2735
}
2836

29-
private resolve(url: string) {
30-
const normalizedUrl = this.handleAbsoluteUrls(url);
37+
resolve(url: string) {
38+
const normalizedUrl = this.resolveRelativeUrls(url);
3139

32-
if (File.exists(normalizedUrl)) {
40+
if (this.fs.fileExists(normalizedUrl)) {
3341
return normalizedUrl;
3442
}
3543

@@ -41,7 +49,7 @@ export class FileSystemResourceLoader extends ResourceLoader {
4149
});
4250

4351
for (let i = 0; i < fallbackCandidates.length; i++) {
44-
if (File.exists(fallbackCandidates[i])) {
52+
if (this.fs.fileExists(fallbackCandidates[i])) {
4553
return fallbackCandidates[i];
4654
}
4755
}

Diff for: tests/app/tests/ns-location-strategy.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FakeFrame extends View implements Frame {
1919
}
2020
}
2121

22-
navigate(entry: NavigationEntry) { }
22+
navigate(entry: any) { }
2323

2424
constructor(private backCB?: () => void) {
2525
super();

Diff for: tests/app/tests/xhr-paths.ts

+54-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,68 @@
11
// make sure you import mocha-config before @angular/core
2-
import {assert} from "./test-config";
3-
import {FileSystemResourceLoader} from "nativescript-angular/resource-loader";
2+
import { assert } from "./test-config";
3+
import { FileSystemResourceLoader } from "nativescript-angular/resource-loader";
4+
5+
import { File } from "tns-core-modules/file-system";
6+
import { NSFileSystem } from "nativescript-angular/file-system/ns-file-system";
7+
8+
class NSFileSystemMock {
9+
public currentApp(): any {
10+
return { path: "/app/dir" };
11+
}
12+
13+
public fileFromPath(path: string): File {
14+
return null;
15+
}
16+
17+
public fileExists(path: string): boolean {
18+
// mycomponent.html aways exists
19+
// mycomponent.css is the other file
20+
return path.indexOf("mycomponent.html") >= 0 || path === "/app/dir/mycomponent.css";
21+
}
22+
}
23+
const fsMock = new NSFileSystemMock();
424

525
describe("XHR name resolution", () => {
26+
let resourceLoader: FileSystemResourceLoader;
27+
before(() => {
28+
resourceLoader = new FileSystemResourceLoader(new NSFileSystemMock());
29+
});
30+
631
it("resolves relative paths from app root", () => {
7-
const xhr = new FileSystemResourceLoader();
8-
assert.strictEqual("/app/dir/mydir/mycomponent.html", xhr.resolve("mydir/mycomponent.html", "/app/dir"));
32+
assert.strictEqual("/app/dir/mydir/mycomponent.html", resourceLoader.resolve("mydir/mycomponent.html"));
933
});
1034

1135
it("resolves double-slashed absolute paths as is", () => {
12-
const xhr = new FileSystemResourceLoader();
13-
assert.strictEqual("//app/mydir/mycomponent.html", xhr.resolve("//app/mydir/mycomponent.html", "/app/dir"));
36+
assert.strictEqual("//app/mydir/mycomponent.html", resourceLoader.resolve("//app/mydir/mycomponent.html"));
1437
});
1538

1639
it("resolves single-slashed absolute paths as is", () => {
17-
const xhr = new FileSystemResourceLoader();
1840
assert.strictEqual(
1941
"/data/data/app/mydir/mycomponent.html",
20-
xhr.resolve("/data/data/app/mydir/mycomponent.html", "/app/dir"));
42+
resourceLoader.resolve("/data/data/app/mydir/mycomponent.html"));
43+
});
44+
45+
it("resolves existing CSS file", () => {
46+
assert.strictEqual(
47+
"/app/dir/mycomponent.css",
48+
resourceLoader.resolve("mycomponent.css"));
49+
});
50+
51+
it("resolves non-existing .scss file to existing .css file", () => {
52+
assert.strictEqual(
53+
"/app/dir/mycomponent.css",
54+
resourceLoader.resolve("mycomponent.scss"));
55+
});
56+
57+
it("resolves non-existing .sass file to existing .css file", () => {
58+
assert.strictEqual(
59+
"/app/dir/mycomponent.css",
60+
resourceLoader.resolve("mycomponent.sass"));
61+
});
62+
63+
it("resolves non-existing .less file to existing .css file", () => {
64+
assert.strictEqual(
65+
"/app/dir/mycomponent.css",
66+
resourceLoader.resolve("mycomponent.less"));
2167
});
2268
});

0 commit comments

Comments
 (0)