| @@ -17,10 +17,6 @@ class DocsDocTest < MiniTest::Spec · 16 unmodified lines |
| 17 | Docs::Entry.new | 17 | Docs::Entry.new |
| 18 | end | 18 | end |
| 19 | | 19 | |
| 20 | let :index do | | |
| 21 | Docs::EntryIndex.new | | |
| 22 | end | | |
| 23 | | | |
| 24 | let :store do | 20 | let :store do |
| 25 | Docs::NullStore.new | 21 | Docs::NullStore.new |
| 26 | end | 22 | end |
| @@ -184,18 +180,18 @@ class DocsDocTest < MiniTest::Spec · 157 unmodified lines |
| 184 | end | 180 | end |
| 185 | end | 181 | end |
| 186 | | 182 | |
| 187 | describe ".index_pages" do | 183 | describe ".store_pages" do |
| 188 | it "build the pages" do | 184 | it "build the pages" do |
| 189 | any_instance_of(doc) do |instance| | 185 | any_instance_of(doc) do |instance| |
| 190 | stub(instance).build_pages { @called = true } | 186 | stub(instance).build_pages { @called = true } |
| 191 | end | 187 | end |
| 192 | doc.index_pages {} | 188 | doc.store_pages(store) {} |
| 193 | assert @called | 189 | assert @called |
| 194 | end | 190 | end |
| 195 | | 191 | |
| 196 | context "when pages are built successfully" do | 192 | context "when pages are built successfully" do |
| 197 | let :pages do | 193 | let :pages do |
| 198 | [page, page.dup] | 194 | [page.dup, page.dup] |
| 199 | end | 195 | end |
| 200 | | 196 | |
| 201 | before do | 197 | before do |
| @@ -204,39 +200,51 @@ class DocsDocTest < MiniTest::Spec · 2 unmodified lines |
| 204 | end | 200 | end |
| 205 | end | 201 | end |
| 206 | | 202 | |
| 207 | it "yields pages that have :entries" do | 203 | context "and at least one page has :entries" do |
| 208 | doc.index_pages { |*args| (@args ||= []) << args } | 204 | it "returns true" do |
| 209 | assert_equal pages.length, @args.length | 205 | assert doc.store_pages(store) |
| 210 | assert_equal [page[:store_path], page[:output]], @args.first | 206 | end |
| 211 | end | | |
| 212 | | 207 | |
| 213 | it "doesn't yield pages that don't have :entries" do | 208 | it "stores a file for each page that has :entries" do |
| 214 | pages.first[:entries] = [] | 209 | pages.first.merge!(entries: [], output: '') |
| 215 | doc.index_pages { |*args| (@args ||= []) << args } | 210 | mock(store).write(page[:store_path], page[:output]) |
| 216 | assert_equal pages.length - 1, @args.length | 211 | mock(store).write('index.json', anything) |
| 217 | end | 212 | doc.store_pages(store) |
| | 213 | end |
| 218 | | 214 | |
| 219 | describe "and at least one has :entries" do | 215 | it "stores an index that contains all the pages' entries" do |
| 220 | it "returns an EntryIndex" do | 216 | stub(store).write(page[:store_path], page[:output]) |
| 221 | assert_instance_of Docs::EntryIndex, doc.index_pages {} | 217 | mock(store).write('index.json', anything) do |path, json| |
| | 218 | json = JSON.parse(json) |
| | 219 | assert_equal pages.length, json['entries'].length |
| | 220 | assert_includes json['entries'], entry.as_json.stringify_keys |
| | 221 | end |
| | 222 | doc.store_pages(store) |
| 222 | end | 223 | end |
| 223 | | 224 | |
| 224 | describe "the index" do | 225 | it "replaces the .path directory before storing the files" do |
| 225 | it "contains all the pages' entries" do | 226 | stub(doc).path { 'path' } |
| 226 | index = doc.index_pages {} | 227 | stub(store).write { assert false } |
| 227 | assert_equal pages.length, index.entries.length | 228 | mock(store).replace('path') do |_, block| |
| 228 | assert_includes index.entries, entry | 229 | stub(store).write |
| | 230 | block.call |
| 229 | end | 231 | end |
| | 232 | doc.store_pages(store) |
| 230 | end | 233 | end |
| 231 | end | 234 | end |
| 232 | | 235 | |
| 233 | context "and none have :entries" do | 236 | context "and no pages have :entries" do |
| 234 | before do | 237 | before do |
| 235 | pages.each { |page| page[:entries] = [] } | 238 | pages.each { |page| page[:entries] = [] } |
| 236 | end | 239 | end |
| 237 | | 240 | |
| 238 | it "returns nil" do | 241 | it "returns false" do |
| 239 | assert_nil doc.index_pages {} | 242 | refute doc.store_pages(store) |
| | 243 | end |
| | 244 | |
| | 245 | it "doesn't store files" do |
| | 246 | dont_allow(store).write |
| | 247 | doc.store_pages(store) |
| 240 | end | 248 | end |
| 241 | end | 249 | end |
| 242 | end | 250 | end |
| @@ -248,57 +256,6 @@ class DocsDocTest < MiniTest::Spec · 5 unmodified lines |
| 248 | end | 256 | end |
| 249 | end | 257 | end |
| 250 | | 258 | |
| 251 | it "doesn't yield" do | | |
| 252 | doc.index_pages { |*_| @yield = true } | | |
| 253 | refute @yield | | |
| 254 | end | | |
| 255 | | | |
| 256 | it "returns nil" do | | |
| 257 | assert_nil doc.index_pages {} | | |
| 258 | end | | |
| 259 | end | | |
| 260 | end | | |
| 261 | | | |
| 262 | describe ".store_pages" do | | |
| 263 | context "when pages are indexed successfully" do | | |
| 264 | before do | | |
| 265 | stub(store).write | | |
| 266 | stub(doc).index_pages do |block| | | |
| 267 | 2.times { block.call page[:store_path], page[:output] } | | |
| 268 | index | | |
| 269 | end | | |
| 270 | end | | |
| 271 | | | |
| 272 | it "returns true" do | | |
| 273 | assert doc.store_pages(store) | | |
| 274 | end | | |
| 275 | | | |
| 276 | it "stores a file for each page" do | | |
| 277 | 2.times { mock(store).write(page[:store_path], page[:output]) } | | |
| 278 | doc.store_pages(store) | | |
| 279 | end | | |
| 280 | | | |
| 281 | it "stores the index" do | | |
| 282 | mock(store).write('index.json', index.to_json) | | |
| 283 | doc.store_pages(store) | | |
| 284 | end | | |
| 285 | | | |
| 286 | it "replaces the .path directory before storing the files" do | | |
| 287 | stub(doc).path { 'path' } | | |
| 288 | stub(store).write { assert false } | | |
| 289 | mock(store).replace('path') do |_, block| | | |
| 290 | stub(store).write | | |
| 291 | block.call | | |
| 292 | end | | |
| 293 | doc.store_pages(store) | | |
| 294 | end | | |
| 295 | end | | |
| 296 | | | |
| 297 | context "when no pages are indexed successfully" do | | |
| 298 | before do | | |
| 299 | stub(doc).index_pages { nil } | | |
| 300 | end | | |
| 301 | | | |
| 302 | it "returns false" do | 259 | it "returns false" do |
| 303 | refute doc.store_pages(store) | 260 | refute doc.store_pages(store) |
| 304 | end | 261 | end |