UNPKG

7.32 kBJavaScriptView Raw
1var fs = require('fs');
2var models = require("../../lib/models");
3
4var m;
5
6models.use(Git);
7
8describe ("Models", function () {
9
10 afterEach(function () {
11 m.configOverride();
12 Git._content = "";
13 });
14
15 function getModel(name, revision) {
16 return new models.Page(name, revision);
17 }
18
19 describe("Page model", function () {
20
21 it ("should initialize the model", function () {
22
23 m = getModel("grazie cara");
24
25 expect(m.name).to.equal("grazie cara");
26 expect(m.wikiname).to.equal("grazie-cara");
27 expect(m.filename).to.equal("grazie-cara.md");
28 expect(m.pathname).to.equal("grazie-cara.md");
29 expect(m.revision).to.equal("HEAD");
30
31 });
32
33 describe("Remove method", function () {
34
35 it ("should delete a file", function (done) {
36
37 m = getModel("verguenza");
38 m.remove().then(function() {
39 expect(Git.rm.called).to.be.true;
40 done();
41 });
42 });
43 });
44
45 describe("Rename method", function () {
46
47 it ("should not rename if the destination exists", function (done) {
48 var stub0 = sinon.stub(fs, 'existsSync').returns(true);
49 m = getModel("verguenza");
50 m.renameTo('vergogna').catch(function() {
51 stub0.restore();
52 done();
53 });
54 });
55
56 it ("should rename if the destination does not exist", function (done) {
57 var stub0 = sinon.stub(fs, 'existsSync').returns(false);
58 m = getModel("verguenza");
59 m.renameTo('vergogna').then(function () {
60 expect(m.name).to.equal("vergogna");
61 expect(m.wikiname).to.equal("vergogna");
62 expect(m.filename).to.equal("vergogna.md");
63 expect(m.pathname).to.equal("vergogna.md");
64 done();
65 });
66 });
67 });
68
69 describe("Save method", function () {
70
71 it ("should save the right content with the default config", function (done) {
72
73 m = getModel("verguenza");
74 m.title = "The huge";
75 m.content = "The verge";
76 var stub0 = sinon.stub(fs, 'writeFile').callsArgOn(2, m);
77 m.save().then(function (content) {
78 expect(content).to.equal("The verge");
79 stub0.restore();
80 done();
81 });
82 });
83
84 it ("should save the right content with the title in the content", function (done) {
85
86 m = getModel("verguenza");
87
88 m.configOverride({
89 pages: {
90 title: {
91 fromFilename: false,
92 fromContent: true
93 }
94 }
95 });
96
97 m.title = "The huge";
98 m.content = "The verge";
99 var stub0 = sinon.stub(fs, 'writeFile').callsArgOn(2, m);
100 m.save().then(function (content) {
101 expect(content).to.equal("# The huge\nThe verge");
102 stub0.restore();
103 done();
104 });
105 });
106 });
107
108 describe("UrlFor method", function () {
109
110 it ("should generate the correct url for page actions", function () {
111
112 m = getModel("verguenza");
113
114 expect(m.urlFor('show')).to.equal('/wiki/verguenza');
115 expect(m.urlFor('edit')).to.equal('/pages/verguenza/edit');
116 expect(m.urlFor('edit error')).to.equal('/pages/verguenza/edit?e=1');
117 expect(m.urlFor('edit put')).to.equal('/pages/verguenza');
118 expect(m.urlFor('history')).to.equal('/wiki/verguenza/history');
119 expect(m.urlFor('compare')).to.equal('/wiki/verguenza/compare');
120 expect(m.urlFor('new')).to.equal('/pages/new/verguenza');
121 expect(m.urlFor('new error')).to.equal('/pages/new/verguenza?e=1');
122 });
123
124 });
125
126 describe("isIndex method", function () {
127
128 it ("should test the correct value for the index", function () {
129
130 m = getModel("pisquanio");
131
132 m.configOverride({
133 pages: {
134 index: "pisquanio"
135 }
136 });
137
138 expect(m.isIndex()).to.be.true;
139 });
140 });
141
142 describe("isFooter method", function () {
143
144 it ("should test the correct value for the footer", function () {
145
146 m = getModel("_footer");
147
148 expect(m.isFooter()).to.be.true;
149 });
150 });
151
152 describe("isSidebar method", function () {
153
154 it ("should test the correct value for the sidebar", function () {
155
156 m = getModel("_sidebar");
157
158 expect(m.isSidebar()).to.be.true;
159 });
160 });
161
162 describe("lock method", function () {
163
164 it ("should lock a page", function () {
165
166 m = getModel("panchovilla");
167
168 var l = m.lock({
169 asGitAuthor: "geronimo@somewhere.com"
170 });
171
172 expect(l).to.be.true;
173 expect(m.lockedBy.asGitAuthor).to.equal("geronimo@somewhere.com");
174
175 l = m.lock({
176 asGitAuthor: "someoneelse@somewhere.com"
177 });
178
179 expect(l).to.be.false;
180 expect(m.lockedBy.asGitAuthor).to.equal("geronimo@somewhere.com");
181 });
182 });
183
184 describe("unlock method", function () {
185
186 it ("should unlock a page", function () {
187
188 m = getModel("panchovilla");
189
190 m.lock({
191 asGitAuthor: "geronimo@somewhere.com"
192 });
193
194 m.unlock();
195
196 expect(m.lockedBy).to.equal(null);
197 });
198 });
199
200 describe("fetchContent method", function () {
201
202 it ("should fetch the content for a page with no content retrieved", function (done) {
203
204 m = getModel("panchovilla");
205
206 m.fetchContent().then(function () {
207 expect(m.content).to.equal("");
208 expect(m.rawContent).to.equal("");
209 expect(m.title).to.equal(m.name);
210 done();
211 })
212 });
213
214 it ("should fetch the content for a page with content retrieved 1", function (done) {
215
216 Git._content = "Bella giornata!";
217
218 m = getModel("panchovilla");
219
220 m.fetchContent().then(function () {
221 expect(m.content).to.equal("Bella giornata!");
222 expect(m.rawContent).to.equal("Bella giornata!");
223 expect(m.title).to.equal(m.name);
224 done();
225 });
226 });
227
228 it ("should fetch the content for a page with content retrieved 2", function (done) {
229
230 // Not the Jingo convention for title in the content
231 Git._content = "Bella serata!";
232
233 m = getModel("panchovilla");
234
235 m.configOverride({
236 pages: {
237 title: {
238 fromFilename: false,
239 fromContent: true
240 }
241 }
242 });
243
244 m.fetchContent().then(function () {
245 expect(m.content).to.equal("Bella serata!");
246 expect(m.rawContent).to.equal("Bella serata!");
247 expect(m.title).to.equal(m.name);
248 done();
249 });
250 });
251
252 it ("should fetch the content for a page with content retrieved 3", function (done) {
253
254 // Jingo convention for title in the content
255 Git._content = "# A nice title\nand some content!";
256
257 m = getModel("panchovilla");
258
259 m.configOverride({
260 pages: {
261 title: {
262 fromFilename: false,
263 fromContent: true
264 }
265 }
266 });
267
268 m.fetchContent().then(function () {
269 expect(m.content).to.equal("and some content!");
270 expect(m.rawContent).to.equal("# A nice title\nand some content!");
271 expect(m.title).to.equal("A nice title");
272 done();
273 });
274 });
275 });
276 });
277});