doc_test.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 ".index_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.index_page('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. it "yields the page's :store_path and :output" do
  112. doc.index_page('') { |*args| @args = args }
  113. assert_equal [page[:store_path], page[:output]], @args
  114. end
  115. it "returns an EntryIndex" do
  116. assert_instance_of Docs::EntryIndex, doc.index_page('') {}
  117. end
  118. describe "the index" do
  119. it "contains the page's entries" do
  120. index = doc.index_page('') {}
  121. assert_equal page[:entries], index.entries
  122. end
  123. end
  124. end
  125. context "when the page doesn't build successfully" do
  126. before do
  127. any_instance_of(doc) do |instance|
  128. stub(instance).build_page { nil }
  129. end
  130. end
  131. it "doesn't yield" do
  132. doc.index_page('') { |*_| @yield = true }
  133. refute @yield
  134. end
  135. it "returns nil" do
  136. assert_nil doc.index_page('') {}
  137. end
  138. end
  139. end
  140. describe ".index_pages" do
  141. it "build the pages" do
  142. any_instance_of(doc) do |instance|
  143. stub(instance).build_pages { @called = true }
  144. end
  145. doc.index_pages {}
  146. assert @called
  147. end
  148. context "when pages are built successfully" do
  149. let :pages do
  150. [page, page.dup]
  151. end
  152. before do
  153. any_instance_of(doc) do |instance|
  154. stub(instance).build_pages { |block| pages.each(&block) }
  155. end
  156. end
  157. it "yields each page's :store_path and :output" do
  158. doc.index_pages { |*args| (@args ||= []) << args }
  159. assert_equal pages.length, @args.length
  160. assert_equal [page[:store_path], page[:output]], @args.first
  161. end
  162. it "returns an EntryIndex" do
  163. assert_instance_of Docs::EntryIndex, doc.index_pages {}
  164. end
  165. describe "the index" do
  166. it "contains all pages' entries" do
  167. index = doc.index_pages {}
  168. assert_equal pages.length, index.entries.length
  169. assert_includes index.entries, entry
  170. end
  171. end
  172. end
  173. context "when no pages are built successfully" do
  174. before do
  175. any_instance_of(doc) do |instance|
  176. stub(instance).build_pages
  177. end
  178. end
  179. it "doesn't yield" do
  180. doc.index_pages { |*_| @yield = true }
  181. refute @yield
  182. end
  183. it "returns nil" do
  184. assert_nil doc.index_pages {}
  185. end
  186. end
  187. end
  188. describe ".store_page" do
  189. context "when the page is indexed successfully" do
  190. before do
  191. stub(doc).index_page('id').yields(page[:store_path], page[:output]) { index }
  192. end
  193. it "returns true" do
  194. assert doc.store_page(store, 'id')
  195. end
  196. it "stores a file" do
  197. mock(store).write(page[:store_path], page[:output])
  198. doc.store_page(store, 'id')
  199. end
  200. it "opens the .path directory before storing the file" do
  201. stub(doc).path { 'path' }
  202. stub(store).write { assert false }
  203. mock(store).open('path') do |_, block|
  204. stub(store).write
  205. block.call
  206. end
  207. doc.store_page(store, 'id')
  208. end
  209. end
  210. context "when the page isn't indexed successfully" do
  211. before do
  212. stub(doc).index_page('id') { nil }
  213. end
  214. it "returns false" do
  215. refute doc.store_page(store, 'id')
  216. end
  217. it "doesn't store a file" do
  218. dont_allow(store).write
  219. doc.store_page(store, 'id')
  220. end
  221. end
  222. end
  223. describe ".store_pages" do
  224. context "when pages are indexed successfully" do
  225. before do
  226. stub(store).write
  227. stub(doc).index_pages do |block|
  228. 2.times { block.call page[:store_path], page[:output] }
  229. index
  230. end
  231. end
  232. it "returns true" do
  233. assert doc.store_pages(store)
  234. end
  235. it "stores a file for each page" do
  236. 2.times { mock(store).write(page[:store_path], page[:output]) }
  237. doc.store_pages(store)
  238. end
  239. it "stores the index" do
  240. mock(store).write('index.json', index.to_json)
  241. doc.store_pages(store)
  242. end
  243. it "replaces the .path directory before storing the files" do
  244. stub(doc).path { 'path' }
  245. stub(store).write { assert false }
  246. mock(store).replace('path') do |_, block|
  247. stub(store).write
  248. block.call
  249. end
  250. doc.store_pages(store)
  251. end
  252. end
  253. context "when no pages are indexed successfully" do
  254. before do
  255. stub(doc).index_pages { nil }
  256. end
  257. it "returns false" do
  258. refute doc.store_pages(store)
  259. end
  260. it "doesn't store files" do
  261. dont_allow(store).write
  262. doc.store_pages(store)
  263. end
  264. end
  265. end
  266. end