1
0

manifest_test.rb 1.9 KB

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