Skip to content

Commit

Permalink
tests: adjust to match the new code
Browse files Browse the repository at this point in the history
  • Loading branch information
Massolari committed Mar 5, 2024
1 parent e94cfd3 commit dfd8c85
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 14 deletions.
51 changes: 37 additions & 14 deletions tests/forem-nvim/setup_spec.fnl
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
(describe :Setup (fn []
(require :forem-nvim)))
(local stub (require :luassert.stub))
(local spy (require :luassert.spy))

(it "should expose my_articles when has an api key"
(fn []
(local forem-nvim (require :forem-nvim))
(forem-nvim.setup {:api_key :blabla})
(assert.truthy forem-nvim.my_articles)))

(it "should not expose my_articles when has no api key"
(fn []
(tset package.loaded :forem-nvim nil)
(local forem-nvim (require :forem-nvim))
(forem-nvim.setup {})
(assert.falsy forem-nvim.my_articles)))
(describe :Setup
(fn []
(var forem-nvim nil)
(before_each (fn []
(set vim.env.FOREM_API_KEY :foo)
(set package.loaded.forem-nvim nil)
(set forem-nvim (require :forem-nvim))
(forem-nvim.setup)))
(it "should show a notification when no api key is set"
(fn []
;; Clear the api key
(set vim.env.FOREM_API_KEY nil)
(stub vim :notify)
(forem-nvim.my_articles)
(let [assert-stub (assert.stub vim.notify)]
(assert-stub.was.called))))
(it "should call the api to get the articles"
(fn []
;; To check if the api is called, we need to mock the api
;; The first step is to clear the api from the package.loaded table
(set package.loaded.forem-nvim nil)
(tset package.loaded :forem-nvim.api nil)
;; Then we can mock the api
(local mocked-api (require :forem-nvim.api))
;; We need to mock the function that we want to check
(set mocked-api.my-articles (spy.new (fn [])))
;; Then we can require the package
(local forem-nvim-api-mocked (require :forem-nvim))
(forem-nvim-api-mocked.setup)
(forem-nvim-api-mocked.my_articles)
;; Finally we can check if the function was called
(let [api-spy (assert.spy mocked-api.my-articles)]
(api-spy.was.called))
;; We need to clear the api from the package.loaded table to reset the state
(tset package.loaded :forem-nvim.api nil)))))
39 changes: 39 additions & 0 deletions tests/forem-nvim/setup_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local stub = require("luassert.stub")
local spy = require("luassert.spy")
local function _1_()
local forem_nvim = nil
local function _2_()
vim.env.FOREM_API_KEY = "foo"
package.loaded["forem-nvim"] = nil
forem_nvim = require("forem-nvim")
return forem_nvim.setup()
end
before_each(_2_)
local function _3_()
vim.env.FOREM_API_KEY = nil
stub(vim, "notify")
forem_nvim.my_articles()
local assert_stub = assert.stub(vim.notify)
return assert_stub.was.called()
end
it("should show a notification when no api key is set", _3_)
local function _4_()
package.loaded["forem-nvim"] = nil
package.loaded["forem-nvim.api"] = nil
local mocked_api = require("forem-nvim.api")
local function _5_()
end
mocked_api["my-articles"] = spy.new(_5_)
local forem_nvim_api_mocked = require("forem-nvim")
forem_nvim_api_mocked.setup()
forem_nvim_api_mocked.my_articles()
do
local api_spy = assert.spy(mocked_api["my-articles"])
api_spy.was.called()
end
package.loaded["forem-nvim.api"] = nil
return nil
end
return it("should call the api to get the articles", _4_)
end
return describe("Setup", _1_)
19 changes: 19 additions & 0 deletions tests/minimal_init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local function load_module(module, source, _3fdirectory)
_G.assert((nil ~= source), "Missing argument source on tests/minimal_init.fnl:1")
_G.assert((nil ~= module), "Missing argument module on tests/minimal_init.fnl:1")
local module_dir = (_3fdirectory or ("/tmp/" .. module))
local is_not_a_directory = (vim.fn.isdirectory(module_dir) == 0)
if is_not_a_directory then
vim.fn.system({"git", "clone", source, module_dir})
else
end
return module_dir
end
local plenary_dir = (os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim")
load_module("plenary.nvim", "https://github.com/nvim-lua/plenary.nvim", plenary_dir)
local telescope_dir = load_module("telescope.nvim", "https://github.com/nvim-telescope/telescope.nvim")
do end (vim.opt.rtp):append(".")
do end (vim.opt.rtp):append(plenary_dir)
do end (vim.opt.rtp):append(telescope_dir)
vim.cmd("runtime plugin/plenary.vim")
return require("plenary.busted")

0 comments on commit dfd8c85

Please sign in to comment.