github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/tests/unit/utils/toc-test.js (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 import toc from 'documize/utils/toc'; 13 import models from 'documize/utils/model'; 14 import { module, test } from 'qunit'; 15 16 module('Unit | Utility | toc'); 17 18 test('toc can only move down', function (assert) { 19 let pages = []; 20 21 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 1024 })); //testing 22 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 1024 * 2 })); 23 24 let state = toc.getState(pages, pages[0]); 25 assert.equal(state.tocTools.upTarget, '', 'Has no up target'); 26 assert.equal(state.tocTools.downTarget, '2', 'Has down target'); 27 assert.equal(state.tocTools.allowIndent, false, 'Cannot indent'); 28 assert.equal(state.tocTools.allowOutdent, false, 'Cannot outdent'); 29 }); 30 31 test('toc can move up or indent', function (assert) { 32 let pages = []; 33 34 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 1024 })); 35 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 1024 * 2 })); //testing 36 37 let state = toc.getState(pages, pages[1]); 38 assert.equal(state.tocTools.upTarget, '1', 'Has up target'); 39 assert.equal(state.tocTools.downTarget, '', 'Has no down target'); 40 assert.equal(state.tocTools.allowIndent, true, 'Can indent'); 41 assert.equal(state.tocTools.allowOutdent, false, 'Cannot outdent'); 42 }); 43 44 test('toc can only outdent', function (assert) { 45 let pages = []; 46 47 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 1024 })); 48 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 1024 * 2 })); 49 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 1024 * 3 })); // testing 50 pages.pushObject(models.PageModel.create({ id: "4", level: 1, sequence: 1024 * 4 })); 51 52 let state = toc.getState(pages, pages[2]); 53 assert.equal(state.tocTools.upTarget, '', 'Has no up target'); 54 assert.equal(state.tocTools.downTarget, '', 'Has no down target'); 55 assert.equal(state.tocTools.allowIndent, false, 'Cannot indent'); 56 assert.equal(state.tocTools.allowOutdent, true, 'Can outdent'); 57 }); 58 59 test('toc child can move up or indent', function (assert) { 60 let pages = []; 61 62 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 1024 })); 63 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 1024 * 2 })); 64 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 1024 * 3 })); // testing 65 pages.pushObject(models.PageModel.create({ id: "4", level: 1, sequence: 1024 * 4 })); 66 67 let page = pages[3]; 68 let state = toc.getState(pages, page); 69 assert.equal(state.tocTools.upTarget, '2', 'Has up target'); 70 assert.equal(state.tocTools.downTarget, '', 'Has no down target'); 71 assert.equal(state.tocTools.allowIndent, true, 'Can indent'); 72 assert.equal(state.tocTools.allowOutdent, false, 'Cannot outdent'); 73 74 let pendingChanges = toc.indent(state, pages, page); 75 assert.equal(pendingChanges.length, 1, 'Has 1 pending change'); 76 assert.equal(pendingChanges[0].pageId, 4); 77 assert.equal(pendingChanges[0].level, 2); 78 }); 79 80 test('toc top node can indent two places', function (assert) { 81 let pages = []; 82 83 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 1024 })); 84 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 1024 * 2 })); 85 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 1024 * 3 })); 86 pages.pushObject(models.PageModel.create({ id: "4", level: 3, sequence: 1024 * 4 })); 87 pages.pushObject(models.PageModel.create({ id: "5", level: 1, sequence: 1024 * 5 })); // testing 88 89 let page = pages[4]; 90 let state = toc.getState(pages, page); 91 assert.equal(state.tocTools.upTarget, '2', 'Has up target'); 92 assert.equal(state.tocTools.downTarget, '', 'Has no down target'); 93 assert.equal(state.tocTools.allowIndent, true, 'Can indent'); 94 assert.equal(state.tocTools.allowOutdent, false, 'Cannot outdent'); 95 96 let pendingChanges = toc.indent(state, pages, page); 97 assert.equal(pendingChanges.length, 1, 'Has 1 pending change'); 98 assert.equal(pendingChanges[0].pageId, 5); 99 assert.equal(pendingChanges[0].level, 3); 100 }); 101 102 test('toc top node with kids can indent two places', function (assert) { 103 let pages = []; 104 105 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 1024 })); 106 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 1024 * 2 })); 107 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 1024 * 3 })); 108 pages.pushObject(models.PageModel.create({ id: "4", level: 3, sequence: 1024 * 4 })); 109 pages.pushObject(models.PageModel.create({ id: "5", level: 1, sequence: 1024 * 5 })); // testing 110 pages.pushObject(models.PageModel.create({ id: "6", level: 2, sequence: 1024 * 6 })); // testing 111 pages.pushObject(models.PageModel.create({ id: "7", level: 3, sequence: 1024 * 7 })); // testing 112 113 let page = pages[4]; 114 let state = toc.getState(pages, page); 115 assert.equal(state.tocTools.upTarget, '2', 'Has up target'); 116 assert.equal(state.tocTools.downTarget, '', 'Has no down target'); 117 assert.equal(state.tocTools.allowIndent, true, 'Can indent'); 118 assert.equal(state.tocTools.allowOutdent, false, 'Cannot outdent'); 119 120 let pendingChanges = toc.indent(state, pages, page); 121 assert.equal(pendingChanges.length, 3, 'Has 1 pending change'); 122 assert.equal(pendingChanges[0].pageId, 5); 123 assert.equal(pendingChanges[0].level, 3); 124 assert.equal(pendingChanges[1].pageId, 6); 125 assert.equal(pendingChanges[1].level, 4); 126 assert.equal(pendingChanges[2].pageId, 7); 127 assert.equal(pendingChanges[2].level, 5); 128 }); 129 130 test('toc same level node with kids can indent one place', function (assert) { 131 let pages = []; 132 133 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 1024 })); 134 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 1024 * 2 })); 135 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 1024 * 3 })); 136 pages.pushObject(models.PageModel.create({ id: "4", level: 2, sequence: 1024 * 4 })); // testing 137 pages.pushObject(models.PageModel.create({ id: "5", level: 3, sequence: 1024 * 5 })); 138 pages.pushObject(models.PageModel.create({ id: "6", level: 1, sequence: 1024 * 6 })); 139 pages.pushObject(models.PageModel.create({ id: "7", level: 2, sequence: 1024 * 7 })); 140 141 let page = pages[3]; 142 let state = toc.getState(pages, page); 143 assert.equal(state.tocTools.upTarget, '3', 'Has up target'); 144 assert.equal(state.tocTools.downTarget, '', 'Has no down target'); 145 assert.equal(state.tocTools.allowIndent, true, 'Can indent'); 146 assert.equal(state.tocTools.allowOutdent, true, 'Can outdent'); 147 148 let pendingChanges = toc.indent(state, pages, page); 149 assert.equal(pendingChanges.length, 2, 'Has 2 pending changes'); 150 assert.equal(pendingChanges[0].pageId, 4); 151 assert.equal(pendingChanges[0].level, 3); 152 assert.equal(pendingChanges[1].pageId, 5); 153 assert.equal(pendingChanges[1].level, 4); 154 }); 155 156 test('toc child with deep tree moves correctly', function (assert) { 157 let pages = []; 158 159 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 1024 })); 160 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 1024 * 2 })); 161 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 1024 * 4 })); // testing 162 pages.pushObject(models.PageModel.create({ id: "4", level: 3, sequence: 1024 * 5 })); // testing 163 pages.pushObject(models.PageModel.create({ id: "5", level: 3, sequence: 1024 * 6 })); // testing 164 pages.pushObject(models.PageModel.create({ id: "6", level: 3, sequence: 1024 * 7 })); // testing 165 pages.pushObject(models.PageModel.create({ id: "7", level: 1, sequence: 1024 * 8 })); 166 pages.pushObject(models.PageModel.create({ id: "8", level: 1, sequence: 1024 * 9 })); 167 pages.pushObject(models.PageModel.create({ id: "9", level: 1, sequence: 1024 * 10 })); 168 169 let page = pages[2]; 170 let state = toc.getState(pages, page); 171 172 assert.equal(state.tocTools.upTarget, '', 'Has no up target'); 173 assert.equal(state.tocTools.downTarget, '', 'Has no down target'); 174 assert.equal(state.tocTools.allowIndent, false, 'Cannot indent'); 175 assert.equal(state.tocTools.allowOutdent, true, 'Can outdent'); 176 177 let pendingChanges = toc.outdent(state, pages, page); 178 179 assert.equal(pendingChanges.length, 4, 'Have 4 pending changes'); 180 assert.equal(pendingChanges[0].pageId, 3); 181 assert.equal(pendingChanges[0].level, 1); 182 assert.equal(pendingChanges[1].pageId, 4); 183 assert.equal(pendingChanges[1].level, 2); 184 assert.equal(pendingChanges[2].pageId, 5); 185 assert.equal(pendingChanges[2].level, 2); 186 assert.equal(pendingChanges[3].pageId, 6); 187 assert.equal(pendingChanges[3].level, 2); 188 }); 189 190 test('toc top level node skips down some', function (assert) { 191 let pages = []; 192 193 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 110 })); // testing 194 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 220 })); 195 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 330 })); 196 pages.pushObject(models.PageModel.create({ id: "4", level: 3, sequence: 440 })); 197 pages.pushObject(models.PageModel.create({ id: "5", level: 3, sequence: 550 })); 198 pages.pushObject(models.PageModel.create({ id: "6", level: 3, sequence: 660 })); 199 pages.pushObject(models.PageModel.create({ id: "7", level: 1, sequence: 770 })); 200 pages.pushObject(models.PageModel.create({ id: "8", level: 1, sequence: 880 })); 201 pages.pushObject(models.PageModel.create({ id: "9", level: 1, sequence: 990 })); 202 203 let page = pages[0]; 204 let state = toc.getState(pages, page); 205 206 assert.equal(state.tocTools.upTarget, '', 'Has no up target'); 207 assert.equal(state.tocTools.downTarget, '2', 'Has down target'); 208 assert.equal(state.tocTools.allowIndent, false, 'Cannot indent'); 209 assert.equal(state.tocTools.allowOutdent, false, 'Cannot outdent'); 210 211 let pendingChanges = toc.moveDown(state, pages, page); 212 213 assert.equal(pendingChanges.length, 1, 'Have 1 pending change'); 214 assert.equal(pendingChanges[0].pageId, 1); 215 assert.equal(pendingChanges[0].sequence, (660 + 770) / 2); 216 }); 217 218 test('toc top level node skips up some', function (assert) { 219 let pages = []; 220 221 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 110 })); 222 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 220 })); 223 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 330 })); 224 pages.pushObject(models.PageModel.create({ id: "4", level: 3, sequence: 440 })); 225 pages.pushObject(models.PageModel.create({ id: "5", level: 3, sequence: 550 })); 226 pages.pushObject(models.PageModel.create({ id: "6", level: 3, sequence: 660 })); 227 pages.pushObject(models.PageModel.create({ id: "7", level: 1, sequence: 770 })); // testing 228 pages.pushObject(models.PageModel.create({ id: "8", level: 1, sequence: 880 })); 229 pages.pushObject(models.PageModel.create({ id: "9", level: 1, sequence: 990 })); 230 231 let page = pages[6]; 232 let state = toc.getState(pages, page); 233 assert.equal(state.tocTools.upTarget, '2', 'Has up target'); 234 assert.equal(state.tocTools.downTarget, '8', 'Has down target'); 235 assert.equal(state.tocTools.allowIndent, true, 'Can indent'); 236 assert.equal(state.tocTools.allowOutdent, false, 'Cannot outdent'); 237 238 let pendingChanges = toc.moveUp(state, pages, page); 239 assert.equal(pendingChanges.length, 1, 'Has 1 pending change'); 240 assert.equal(pendingChanges[0].pageId, 7); 241 assert.equal(pendingChanges[0].sequence, (110 + 220) / 2); 242 }); 243 244 test('toc move down top node to bottom', function (assert) { 245 let pages = []; 246 247 pages.pushObject(models.PageModel.create({ id: "1", level: 1, sequence: 110 })); 248 pages.pushObject(models.PageModel.create({ id: "2", level: 1, sequence: 220 })); 249 pages.pushObject(models.PageModel.create({ id: "3", level: 2, sequence: 330 })); 250 251 let page = pages[0]; 252 let state = toc.getState(pages, page); 253 assert.equal(state.tocTools.upTarget, '', 'Has no up target'); 254 assert.equal(state.tocTools.downTarget, '2', 'Has down target'); 255 assert.equal(state.tocTools.allowIndent, false, 'Cannot indent'); 256 assert.equal(state.tocTools.allowOutdent, false, 'Cannot outdent'); 257 258 let pendingChanges = toc.moveDown(state, pages, page); 259 assert.equal(pendingChanges.length, 1, 'Has 1 pending change'); 260 assert.equal(pendingChanges[0].pageId, 1); 261 assert.equal(pendingChanges[0].sequence, 330 * 2); 262 });