doc_test.rb 7.2 KB

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