From dfd8c85f05a52d69cbfee40080ffe5e01750c370 Mon Sep 17 00:00:00 2001 From: "Douglas M." Date: Tue, 5 Mar 2024 21:42:48 +0100 Subject: [PATCH] tests: adjust to match the new code --- tests/forem-nvim/setup_spec.fnl | 51 ++++++++++++++++++++++++--------- tests/forem-nvim/setup_spec.lua | 39 +++++++++++++++++++++++++ tests/minimal_init.lua | 19 ++++++++++++ 3 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 tests/forem-nvim/setup_spec.lua create mode 100644 tests/minimal_init.lua diff --git a/tests/forem-nvim/setup_spec.fnl b/tests/forem-nvim/setup_spec.fnl index ae5d191..812f075 100644 --- a/tests/forem-nvim/setup_spec.fnl +++ b/tests/forem-nvim/setup_spec.fnl @@ -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))))) diff --git a/tests/forem-nvim/setup_spec.lua b/tests/forem-nvim/setup_spec.lua new file mode 100644 index 0000000..170cb2d --- /dev/null +++ b/tests/forem-nvim/setup_spec.lua @@ -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_) diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua new file mode 100644 index 0000000..1c5b52c --- /dev/null +++ b/tests/minimal_init.lua @@ -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")