1 | import * as fse from "fs-extra";
|
2 | import { expect } from "chai";
|
3 | import helper from "./helper";
|
4 | import * as jetpack from "..";
|
5 | import { ExistsResult } from "../types";
|
6 |
|
7 | describe("exists", () => {
|
8 | beforeEach(helper.setCleanTestCwd);
|
9 | afterEach(helper.switchBackToCorrectCwd);
|
10 |
|
11 | describe("returns false if file doesn't exist", () => {
|
12 | const expectations = (exists: ExistsResult) => {
|
13 | expect(exists).to.equal(false);
|
14 | };
|
15 |
|
16 | it("sync", () => {
|
17 | expectations(jetpack.exists("file.txt"));
|
18 | });
|
19 |
|
20 | it("async", done => {
|
21 | jetpack.existsAsync("file.txt").then(exists => {
|
22 | expectations(exists);
|
23 | done();
|
24 | });
|
25 | });
|
26 | });
|
27 |
|
28 | describe("returns 'dir' if directory exists on given path", () => {
|
29 | const preparations = () => {
|
30 | fse.mkdirsSync("a");
|
31 | };
|
32 |
|
33 | const expectations = (exists: ExistsResult) => {
|
34 | expect(exists).to.equal("dir");
|
35 | };
|
36 |
|
37 | it("sync", () => {
|
38 | preparations();
|
39 | expectations(jetpack.exists("a"));
|
40 | });
|
41 |
|
42 | it("async", done => {
|
43 | preparations();
|
44 | jetpack.existsAsync("a").then(exists => {
|
45 | expectations(exists);
|
46 | done();
|
47 | });
|
48 | });
|
49 | });
|
50 |
|
51 | describe("returns 'file' if file exists on given path", () => {
|
52 | const preparations = () => {
|
53 | fse.outputFileSync("text.txt", "abc");
|
54 | };
|
55 |
|
56 | const expectations = (exists: ExistsResult) => {
|
57 | expect(exists).to.equal("file");
|
58 | };
|
59 |
|
60 | it("sync", () => {
|
61 | preparations();
|
62 | expectations(jetpack.exists("text.txt"));
|
63 | });
|
64 |
|
65 | it("async", done => {
|
66 | preparations();
|
67 | jetpack.existsAsync("text.txt").then(exists => {
|
68 | expectations(exists);
|
69 | done();
|
70 | });
|
71 | });
|
72 | });
|
73 |
|
74 | describe("respects internal CWD of jetpack instance", () => {
|
75 | const preparations = () => {
|
76 | fse.outputFileSync("a/text.txt", "abc");
|
77 | };
|
78 |
|
79 | const expectations = (exists: ExistsResult) => {
|
80 | expect(exists).to.equal("file");
|
81 | };
|
82 |
|
83 | it("sync", () => {
|
84 | const jetContext = jetpack.cwd("a");
|
85 | preparations();
|
86 | expectations(jetContext.exists("text.txt"));
|
87 | });
|
88 |
|
89 | it("async", done => {
|
90 | const jetContext = jetpack.cwd("a");
|
91 | preparations();
|
92 | jetContext.existsAsync("text.txt").then(exists => {
|
93 | expectations(exists);
|
94 | done();
|
95 | });
|
96 | });
|
97 | });
|
98 |
|
99 | describe("input validation", () => {
|
100 | const tests = [
|
101 | { type: "sync", method: jetpack.exists, methodName: "exists" },
|
102 | { type: "async", method: jetpack.existsAsync, methodName: "existsAsync" }
|
103 | ];
|
104 |
|
105 | describe('"path" argument', () => {
|
106 | tests.forEach(test => {
|
107 | it(test.type, () => {
|
108 | expect(() => {
|
109 | test.method(undefined);
|
110 | }).to.throw(
|
111 | `Argument "path" passed to ${
|
112 | test.methodName
|
113 | }(path) must be a string. Received undefined`
|
114 | );
|
115 | });
|
116 | });
|
117 | });
|
118 | });
|
119 | });
|