doc_test.rb 7.2 KB

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