|
1 | 1 | // 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 always 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(); |
4 | 24 |
|
5 | 25 | describe("XHR name resolution", () => {
|
| 26 | + let resourceLoader: FileSystemResourceLoader; |
| 27 | + before(() => { |
| 28 | + resourceLoader = new FileSystemResourceLoader(new NSFileSystemMock()); |
| 29 | + }); |
| 30 | + |
6 | 31 | 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")); |
9 | 33 | });
|
10 | 34 |
|
11 | 35 | 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")); |
14 | 37 | });
|
15 | 38 |
|
16 | 39 | it("resolves single-slashed absolute paths as is", () => {
|
17 |
| - const xhr = new FileSystemResourceLoader(); |
18 | 40 | assert.strictEqual(
|
19 | 41 | "/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")); |
| 67 | + }); |
| 68 | + |
| 69 | + it("throws for non-existing file that has no fallbacks", () => { |
| 70 | + assert.throws(() => resourceLoader.resolve("does-not-exist.css")); |
21 | 71 | });
|
22 | 72 | });
|
0 commit comments