UNPKG

3.6 kBPlain TextView Raw
1import * as pathUtil from "path";
2import * as fse from "fs-extra";
3import { expect } from "chai";
4import path from "./assert_path";
5import helper from "./helper";
6import * as jetpack from "..";
7
8describe("rename", () => {
9 beforeEach(helper.setCleanTestCwd);
10 afterEach(helper.switchBackToCorrectCwd);
11
12 describe("renames file", () => {
13 const preparations = () => {
14 fse.outputFileSync("a/b.txt", "abc");
15 };
16
17 const expectations = () => {
18 path("a/b.txt").shouldNotExist();
19 path("a/x.txt").shouldBeFileWithContent("abc");
20 };
21
22 it("sync", () => {
23 preparations();
24 jetpack.rename("a/b.txt", "x.txt");
25 expectations();
26 });
27
28 it("async", done => {
29 preparations();
30 jetpack.renameAsync("a/b.txt", "x.txt").then(() => {
31 expectations();
32 done();
33 });
34 });
35 });
36
37 describe("renames directory", () => {
38 const preparations = () => {
39 fse.outputFileSync("a/b/c.txt", "abc");
40 };
41
42 const expectations = () => {
43 path("a/b").shouldNotExist();
44 path("a/x").shouldBeDirectory();
45 };
46
47 it("sync", () => {
48 preparations();
49 jetpack.rename("a/b", "x");
50 expectations();
51 });
52
53 it("async", done => {
54 preparations();
55 jetpack.renameAsync("a/b", "x").then(() => {
56 expectations();
57 done();
58 });
59 });
60 });
61
62 describe("respects internal CWD of jetpack instance", () => {
63 const preparations = () => {
64 fse.outputFileSync("a/b/c.txt", "abc");
65 };
66
67 const expectations = () => {
68 path("a/b").shouldNotExist();
69 path("a/x").shouldBeDirectory();
70 };
71
72 it("sync", () => {
73 const jetContext = jetpack.cwd("a");
74 preparations();
75 jetContext.rename("b", "x");
76 expectations();
77 });
78
79 it("async", done => {
80 const jetContext = jetpack.cwd("a");
81 preparations();
82 jetContext.renameAsync("b", "x").then(() => {
83 expectations();
84 done();
85 });
86 });
87 });
88
89 describe("input validation", () => {
90 const tests = [
91 { type: "sync", method: jetpack.rename as any, methodName: "rename" },
92 {
93 type: "async",
94 method: jetpack.renameAsync as any,
95 methodName: "renameAsync"
96 }
97 ];
98
99 describe('"path" argument', () => {
100 tests.forEach(test => {
101 it(test.type, () => {
102 expect(() => {
103 test.method(undefined, "xyz");
104 }).to.throw(
105 `Argument "path" passed to ${
106 test.methodName
107 }(path, newName) must be a string. Received undefined`
108 );
109 });
110 });
111 });
112
113 describe('"newName" argument', () => {
114 describe("type check", () => {
115 tests.forEach(test => {
116 it(test.type, () => {
117 expect(() => {
118 test.method("abc", undefined);
119 }).to.throw(
120 `Argument "newName" passed to ${
121 test.methodName
122 }(path, newName) must be a string. Received undefined`
123 );
124 });
125 });
126 });
127
128 describe("shouldn't be path, just a filename", () => {
129 const pathToTest = pathUtil.join("new-name", "with-a-slash");
130 tests.forEach(test => {
131 it(test.type, () => {
132 expect(() => {
133 test.method("abc", pathToTest);
134 }).to.throw(
135 `Argument "newName" passed to ${
136 test.methodName
137 }(path, newName) should be a filename, not a path. Received "${pathToTest}"`
138 );
139 });
140 });
141 });
142 });
143 });
144});