manifest_test.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. require 'test_helper'
  2. require 'docs'
  3. class ManifestTest < MiniTest::Spec
  4. let :doc do
  5. doc = Class.new Docs::Doc
  6. doc.name = 'TestDoc'
  7. doc
  8. end
  9. let :store do
  10. Docs::NullStore.new
  11. end
  12. let :manifest do
  13. Docs::Manifest.new store, [doc]
  14. end
  15. describe "#store" do
  16. before do
  17. stub(manifest).as_json
  18. end
  19. it "stores a file" do
  20. mock(store).write.with_any_args
  21. manifest.store
  22. end
  23. describe "the file" do
  24. it "is named ::FILENAME" do
  25. mock(store).write Docs::Manifest::FILENAME, anything
  26. manifest.store
  27. end
  28. it "contains the manifest's JSON dump" do
  29. stub(manifest).to_json { 'json' }
  30. mock(store).write anything, 'json'
  31. manifest.store
  32. end
  33. end
  34. end
  35. describe "#as_json" do
  36. let :meta_path do
  37. 'meta_path'
  38. end
  39. before do
  40. stub(doc).meta_path { meta_path }
  41. end
  42. it "returns an array" do
  43. manifest = Docs::Manifest.new store, []
  44. assert_instance_of Array, manifest.as_json
  45. end
  46. context "when the doc has a meta file" do
  47. before do
  48. stub(store).exist?(meta_path) { true }
  49. stub(store).read(meta_path) { '{"name":"Test"}' }
  50. end
  51. it "includes the doc's meta representation" do
  52. json = manifest.as_json
  53. assert_equal 1, json.length
  54. assert_equal 'Test', json[0]['name']
  55. end
  56. end
  57. context "when the doc doesn't have a meta file" do
  58. it "doesn't include the doc" do
  59. stub(store).exist?(meta_path) { false }
  60. assert_empty manifest.as_json
  61. end
  62. end
  63. end
  64. describe "#to_json" do
  65. it "returns the JSON string for #as_json" do
  66. stub(manifest).as_json { { test: 'ok' } }
  67. assert_equal "{\n \"test\": \"ok\"\n}", manifest.to_json
  68. end
  69. end
  70. end