UNPKG

4.17 kBPlain TextView Raw
1import * as fse from "fs-extra";
2import { expect } from "chai";
3import path from "./assert_path";
4import helper from "./helper";
5import * as jetpack from "..";
6
7describe("move", () => {
8 beforeEach(helper.setCleanTestCwd);
9 afterEach(helper.switchBackToCorrectCwd);
10
11 describe("moves file", () => {
12 const preparations = () => {
13 fse.outputFileSync("a/b.txt", "abc");
14 };
15
16 const expectations = () => {
17 path("a/b.txt").shouldNotExist();
18 path("c.txt").shouldBeFileWithContent("abc");
19 };
20
21 it("sync", () => {
22 preparations();
23 jetpack.move("a/b.txt", "c.txt");
24 expectations();
25 });
26
27 it("async", done => {
28 preparations();
29 jetpack.moveAsync("a/b.txt", "c.txt").then(() => {
30 expectations();
31 done();
32 });
33 });
34 });
35
36 describe("moves directory", () => {
37 const preparations = () => {
38 fse.outputFileSync("a/b/c.txt", "abc");
39 fse.mkdirsSync("x");
40 };
41
42 const expectations = () => {
43 path("a").shouldNotExist();
44 path("x/y/b/c.txt").shouldBeFileWithContent("abc");
45 };
46
47 it("sync", () => {
48 preparations();
49 jetpack.move("a", "x/y");
50 expectations();
51 });
52
53 it("async", done => {
54 preparations();
55 jetpack.moveAsync("a", "x/y").then(() => {
56 expectations();
57 done();
58 });
59 });
60 });
61
62 describe("creates nonexistent directories for destination path", () => {
63 const preparations = () => {
64 fse.outputFileSync("a.txt", "abc");
65 };
66
67 const expectations = () => {
68 path("a.txt").shouldNotExist();
69 path("a/b/z.txt").shouldBeFileWithContent("abc");
70 };
71
72 it("sync", () => {
73 preparations();
74 jetpack.move("a.txt", "a/b/z.txt");
75 expectations();
76 });
77
78 it("async", done => {
79 preparations();
80 jetpack.moveAsync("a.txt", "a/b/z.txt").then(() => {
81 expectations();
82 done();
83 });
84 });
85 });
86
87 describe("generates nice error when source path doesn't exist", () => {
88 const expectations = (err: any) => {
89 expect(err.code).to.equal("ENOENT");
90 expect(err.message).to.have.string("Path to move doesn't exist");
91 };
92
93 it("sync", () => {
94 try {
95 jetpack.move("a", "b");
96 throw new Error("Expected error to be thrown");
97 } catch (err) {
98 expectations(err);
99 }
100 });
101
102 it("async", done => {
103 jetpack.moveAsync("a", "b").catch(err => {
104 expectations(err);
105 done();
106 });
107 });
108 });
109
110 describe("respects internal CWD of jetpack instance", () => {
111 const preparations = () => {
112 fse.outputFileSync("a/b.txt", "abc");
113 };
114
115 const expectations = () => {
116 path("a/b.txt").shouldNotExist();
117 path("a/x.txt").shouldBeFileWithContent("abc");
118 };
119
120 it("sync", () => {
121 const jetContext = jetpack.cwd("a");
122 preparations();
123 jetContext.move("b.txt", "x.txt");
124 expectations();
125 });
126
127 it("async", done => {
128 const jetContext = jetpack.cwd("a");
129 preparations();
130 jetContext.moveAsync("b.txt", "x.txt").then(() => {
131 expectations();
132 done();
133 });
134 });
135 });
136
137 describe("input validation", () => {
138 const tests = [
139 { type: "sync", method: jetpack.move as any, methodName: "move" },
140 {
141 type: "async",
142 method: jetpack.moveAsync as any,
143 methodName: "moveAsync"
144 }
145 ];
146
147 describe('"from" argument', () => {
148 tests.forEach(test => {
149 it(test.type, () => {
150 expect(() => {
151 test.method(undefined, "xyz");
152 }).to.throw(
153 `Argument "from" passed to ${
154 test.methodName
155 }(from, to) must be a string. Received undefined`
156 );
157 });
158 });
159 });
160
161 describe('"to" argument', () => {
162 tests.forEach(test => {
163 it(test.type, () => {
164 expect(() => {
165 test.method("abc", undefined);
166 }).to.throw(
167 `Argument "to" passed to ${
168 test.methodName
169 }(from, to) must be a string. Received undefined`
170 );
171 });
172 });
173 });
174 });
175});