UNPKG

2.99 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("rename", () => {
8 beforeEach(helper.setCleanTestCwd);
9 afterEach(helper.switchBackToCorrectCwd);
10
11 describe("renames file", () => {
12 const preparations = () => {
13 fse.outputFileSync("a/b.txt", "abc");
14 };
15
16 const expectations = () => {
17 path("a/b.txt").shouldNotExist();
18 path("a/x.txt").shouldBeFileWithContent("abc");
19 };
20
21 it("sync", () => {
22 preparations();
23 jetpack.rename("a/b.txt", "x.txt");
24 expectations();
25 });
26
27 it("async", done => {
28 preparations();
29 jetpack.renameAsync("a/b.txt", "x.txt").then(() => {
30 expectations();
31 done();
32 });
33 });
34 });
35
36 describe("renames directory", () => {
37 const preparations = () => {
38 fse.outputFileSync("a/b/c.txt", "abc");
39 };
40
41 const expectations = () => {
42 path("a/b").shouldNotExist();
43 path("a/x").shouldBeDirectory();
44 };
45
46 it("sync", () => {
47 preparations();
48 jetpack.rename("a/b", "x");
49 expectations();
50 });
51
52 it("async", done => {
53 preparations();
54 jetpack.renameAsync("a/b", "x").then(() => {
55 expectations();
56 done();
57 });
58 });
59 });
60
61 describe("respects internal CWD of jetpack instance", () => {
62 const preparations = () => {
63 fse.outputFileSync("a/b/c.txt", "abc");
64 };
65
66 const expectations = () => {
67 path("a/b").shouldNotExist();
68 path("a/x").shouldBeDirectory();
69 };
70
71 it("sync", () => {
72 const jetContext = jetpack.cwd("a");
73 preparations();
74 jetContext.rename("b", "x");
75 expectations();
76 });
77
78 it("async", done => {
79 const jetContext = jetpack.cwd("a");
80 preparations();
81 jetContext.renameAsync("b", "x").then(() => {
82 expectations();
83 done();
84 });
85 });
86 });
87
88 describe("input validation", () => {
89 const tests = [
90 { type: "sync", method: jetpack.rename as any, methodName: "rename" },
91 {
92 type: "async",
93 method: jetpack.renameAsync as any,
94 methodName: "renameAsync"
95 }
96 ];
97
98 describe('"path" argument', () => {
99 tests.forEach(test => {
100 it(test.type, () => {
101 expect(() => {
102 test.method(undefined, "xyz");
103 }).to.throw(
104 `Argument "path" passed to ${
105 test.methodName
106 }(path, newName) must be a string. Received undefined`
107 );
108 });
109 });
110 });
111
112 describe('"newName" argument', () => {
113 tests.forEach(test => {
114 it(test.type, () => {
115 expect(() => {
116 test.method("abc", undefined);
117 }).to.throw(
118 `Argument "newName" passed to ${
119 test.methodName
120 }(path, newName) must be a string. Received undefined`
121 );
122 });
123 });
124 });
125 });
126});