doc_test.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. require 'test_helper'
  2. require 'docs'
  3. class DocsDocTest < MiniTest::Spec
  4. let :doc do
  5. Class.new Docs::Doc do
  6. self.name = 'name'
  7. self.type = 'type'
  8. end
  9. end
  10. let :page do
  11. { store_path: 'store_path', output: 'output', entries: [entry] }
  12. end
  13. let :entry do
  14. Docs::Entry.new
  15. end
  16. let :index do
  17. Docs::EntryIndex.new
  18. end
  19. let :store do
  20. Docs::NullStore.new
  21. end
  22. describe ".inherited" do
  23. it "sets .type" do
  24. assert_equal doc.type, Class.new(doc).type
  25. end
  26. end
  27. describe ".name" do
  28. it "returns 'Doc' when the class is Docs::Doc" do
  29. assert_equal 'Doc', Docs::Doc.name
  30. end
  31. end
  32. describe ".name=" do
  33. it "stores .name" do
  34. doc.name = 'test'
  35. assert_equal 'test', doc.name
  36. end
  37. end
  38. describe ".slug" do
  39. it "returns 'doc' when the class is Docs::Doc" do
  40. assert_equal 'doc', Docs::Doc.slug
  41. end
  42. end
  43. describe ".slug=" do
  44. it "stores .slug" do
  45. doc.slug = 'test'
  46. assert_equal 'test', doc.slug
  47. end
  48. end
  49. describe ".version=" do
  50. it "stores .version" do
  51. doc.version = '1'
  52. assert_equal '1', doc.version
  53. end
  54. end
  55. describe ".abstract" do
  56. it "returns nil" do
  57. assert_nil doc.abstract
  58. end
  59. end
  60. describe ".abstract=" do
  61. it "stores .abstract" do
  62. doc.abstract = true
  63. assert doc.abstract
  64. end
  65. end
  66. describe ".path" do
  67. it "returns .slug" do
  68. doc.slug = 'slug'
  69. assert_equal 'slug', doc.path
  70. end
  71. end
  72. describe ".index_path" do
  73. it "returns .path + ::INDEX_FILENAME" do
  74. stub(doc).path { 'path' }
  75. assert_equal File.join('path', Docs::Doc::INDEX_FILENAME), doc.index_path
  76. end
  77. end
  78. describe ".new" do
  79. it "raises an error when .abstract is true" do
  80. doc.abstract = true
  81. assert_raises NotImplementedError do
  82. doc.new
  83. end
  84. end
  85. end
  86. describe ".as_json" do
  87. it "returns a hash" do
  88. assert_instance_of Hash, doc.as_json
  89. end
  90. it "includes the doc's name, slug, type, version and index_path" do
  91. %w(name slug type version index_path).each do |attribute|
  92. eval "stub(doc).#{attribute} { attribute }"
  93. assert_equal attribute, doc.as_json[attribute.to_sym]
  94. end
  95. end
  96. end
  97. describe ".store_page" do
  98. it "builds a page" do
  99. any_instance_of(doc) do |instance|
  100. stub(instance).build_page('id') { @called = true; nil }
  101. end
  102. doc.store_page(store, 'id') {}
  103. assert @called
  104. end
  105. context "when the page builds successfully" do
  106. before do
  107. any_instance_of(doc) do |instance|
  108. stub(instance).build_page { page }
  109. end
  110. end
  111. context "and it has :entries" do
  112. it "returns true" do
  113. assert doc.store_page(store, 'id')
  114. end
  115. it "stores a file" do
  116. mock(store).write(page[:store_path], page[:output])
  117. doc.store_page(store, 'id')
  118. end
  119. it "opens the .path directory before storing the file" do
  120. stub(doc).path { 'path' }
  121. stub(store).write { assert false }
  122. mock(store).open('path') do |_, block|
  123. stub(store).write
  124. block.call
  125. end
  126. doc.store_page(store, 'id')
  127. end
  128. end
  129. context "and it doesn't have :entries" do
  130. before do
  131. page[:entries] = []
  132. end
  133. it "returns false" do
  134. refute doc.store_page(store, 'id')
  135. end
  136. it "doesn't store a file" do
  137. dont_allow(store).write
  138. doc.store_page(store, 'id')
  139. end
  140. end
  141. end
  142. context "when the page doesn't build successfully" do
  143. before do
  144. any_instance_of(doc) do |instance|
  145. stub(instance).build_page { nil }
  146. end
  147. end
  148. it "returns false" do
  149. refute doc.store_page(store, 'id')
  150. end
  151. it "doesn't store a file" do
  152. dont_allow(store).write
  153. doc.store_page(store, 'id')
  154. end
  155. end
  156. end
  157. describe ".index_pages" do
  158. it "build the pages" do
  159. any_instance_of(doc) do |instance|
  160. stub(instance).build_pages { @called = true }
  161. end
  162. doc.index_pages {}
  163. assert @called
  164. end
  165. context "when pages are built successfully" do
  166. let :pages do
  167. [page, page.dup]
  168. end
  169. before do
  170. any_instance_of(doc) do |instance|
  171. stub(instance).build_pages { |block| pages.each(&block) }
  172. end
  173. end
  174. it "yields pages that have :entries" do
  175. doc.index_pages { |*args| (@args ||= []) << args }
  176. assert_equal pages.length, @args.length
  177. assert_equal [page[:store_path], page[:output]], @args.first
  178. end
  179. it "doesn't yield pages that don't have :entries" do
  180. pages.first[:entries] = []
  181. doc.index_pages { |*args| (@args ||= []) << args }
  182. assert_equal pages.length - 1, @args.length
  183. end
  184. describe "and at least one has :entries" do
  185. it "returns an EntryIndex" do
  186. assert_instance_of Docs::EntryIndex, doc.index_pages {}
  187. end
  188. describe "the index" do
  189. it "contains all the pages' entries" do
  190. index = doc.index_pages {}
  191. assert_equal pages.length, index.entries.length
  192. assert_includes index.entries, entry
  193. end
  194. end
  195. end
  196. context "and none have :entries" do
  197. before do
  198. pages.each { |page| page[:entries] = [] }
  199. end
  200. it "returns nil" do
  201. assert_nil doc.index_pages {}
  202. end
  203. end
  204. end
  205. context "when no pages are built successfully" do
  206. before do
  207. any_instance_of(doc) do |instance|
  208. stub(instance).build_pages
  209. end
  210. end
  211. it "doesn't yield" do
  212. doc.index_pages { |*_| @yield = true }
  213. refute @yield
  214. end
  215. it "returns nil" do
  216. assert_nil doc.index_pages {}
  217. end
  218. end
  219. end
  220. describe ".store_pages" do
  221. context "when pages are indexed successfully" do
  222. before do
  223. stub(store).write
  224. stub(doc).index_pages do |block|
  225. 2.times { block.call page[:store_path], page[:output] }
  226. index
  227. end
  228. end
  229. it "returns true" do
  230. assert doc.store_pages(store)
  231. end
  232. it "stores a file for each page" do
  233. 2.times { mock(store).write(page[:store_path], page[:output]) }
  234. doc.store_pages(store)
  235. end
  236. it "stores the index" do
  237. mock(store).write('index.json', index.to_json)
  238. doc.store_pages(store)
  239. end
  240. it "replaces the .path directory before storing the files" do
  241. stub(doc).path { 'path' }
  242. stub(store).write { assert false }
  243. mock(store).replace('path') do |_, block|
  244. stub(store).write
  245. block.call
  246. end
  247. doc.store_pages(store)
  248. end
  249. end
  250. context "when no pages are indexed successfully" do
  251. before do
  252. stub(doc).index_pages { nil }
  253. end
  254. it "returns false" do
  255. refute doc.store_pages(store)
  256. end
  257. it "doesn't store files" do
  258. dont_allow(store).write
  259. doc.store_pages(store)
  260. end
  261. end
  262. end
  263. end