entry_index_test.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsEntryIndexTest < MiniTest::Spec
  4. let :entry do
  5. Docs::Entry.new 'name', 'type', 'path'
  6. end
  7. let :index do
  8. Docs::EntryIndex.new
  9. end
  10. describe "#entries" do
  11. it "returns an Array" do
  12. assert_instance_of Array, index.entries
  13. end
  14. end
  15. describe "#types" do
  16. it "returns a hash" do
  17. assert_instance_of Hash, index.types
  18. end
  19. end
  20. describe "#add" do
  21. it "stores an entry" do
  22. index.add(entry)
  23. assert_includes index.entries, entry
  24. end
  25. it "stores an array of entries" do
  26. entries = [entry, entry]
  27. index.add(entries)
  28. assert_equal entries, index.entries
  29. end
  30. it "duplicates the entry" do
  31. index.add(entry)
  32. refute_same entry, index.entries.first
  33. end
  34. it "doesn't store the root entry" do
  35. mock(entry).root? { true }
  36. index.add(entry)
  37. assert_empty index.entries
  38. assert_empty index.types
  39. end
  40. it "creates and indexes the type" do
  41. entry.type = 'one'; index.add(entry)
  42. entry.type = 'two'; 2.times { index.add(entry) }
  43. assert_equal ['one', 'two'], index.types.keys
  44. assert_instance_of Docs::Type, index.types['one']
  45. end
  46. it "doesn't index the nil type" do
  47. entry.type = nil; index.add(entry)
  48. assert_empty index.types
  49. end
  50. it "increments the type's count" do
  51. 2.times { index.add(entry) }
  52. assert_equal 2, index.types[entry.type].count
  53. end
  54. end
  55. describe "#empty? / #blank? / #present?" do
  56. it "is #empty? and #blank? when no entries have been added" do
  57. assert index.empty?
  58. assert index.blank?
  59. refute index.present?
  60. end
  61. it "is #present? when an entry has been added" do
  62. index.add(entry)
  63. refute index.empty?
  64. refute index.blank?
  65. assert index.present?
  66. end
  67. end
  68. describe "#as_json" do
  69. it "returns a Hash" do
  70. assert_instance_of Hash, index.as_json
  71. end
  72. describe ":entries" do
  73. it "is an empty array by default" do
  74. assert_instance_of Array, index.as_json[:entries]
  75. end
  76. it "includes the json representation of the #entries" do
  77. index.add [entry, entry]
  78. assert_equal [entry.as_json, entry.as_json], index.as_json[:entries]
  79. end
  80. it "is sorted by name, case-insensitive" do
  81. entry.name = 'B'; index.add(entry)
  82. entry.name = 'a'; index.add(entry)
  83. entry.name = 'c'; index.add(entry)
  84. entry.name = nil; index.add(entry)
  85. assert_equal [nil, 'a', 'B', 'c'], index.as_json[:entries].map { |e| e[:name] }
  86. end
  87. end
  88. describe ":types" do
  89. it "is an empty array by default" do
  90. assert_instance_of Array, index.as_json[:types]
  91. end
  92. it "includes the json representation of the #types" do
  93. type = Docs::Type.new 'one', 1
  94. entry.type = 'one'; index.add(entry)
  95. assert_equal type.as_json, index.as_json[:types].first
  96. end
  97. it "is sorted by name, case-insensitive" do
  98. entry.type = 'B'; index.add(entry)
  99. entry.type = 'a'; index.add(entry)
  100. entry.type = 'c'; index.add(entry)
  101. assert_equal ['a', 'B', 'c'], index.as_json[:types].map { |e| e[:name] }
  102. end
  103. end
  104. end
  105. describe "#to_json" do
  106. it "returns the JSON string for #as_json" do
  107. stub(index).as_json { { entries: [1], types: [2] } }
  108. assert_equal '{"entries":[1],"types":[2]}', index.to_json
  109. end
  110. end
  111. end