Skip to content

Commit

Permalink
tests(event): ensure plugins are loaded only once
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jun 5, 2024
1 parent 9b54ecb commit c17ba22
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lua/lz/n/handler/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,11 @@ function M.add(plugin)
end
end

---@param plugin LzPlugin
function M.del(plugin)
for _, plugins in pairs(M.pending) do
plugins[plugin.name] = nil
end
end

return M
36 changes: 32 additions & 4 deletions spec/event_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local event = require("lz.n.handler.event")
-- local loader = require("lz.n.loader")
local state = require("lz.n.state")
local loader = require("lz.n.loader")
local spy = require("luassert.spy")

describe("event", function()
it("can parse from string", function()
Expand Down Expand Up @@ -39,14 +41,40 @@ describe("event", function()
})
)
end)
it("integration", function()
it("Event should only load plugin once", function()
---@type LzPlugin
local plugin = {
name = "foo",
event = { { id = "BufEnter", event = "BufEnter" } },
event = { event.parse("BufEnter") },
pattern = ".lua",
}
local spy_load = spy.on(loader, "_load")
state.plugins[plugin.name] = plugin
event.add(plugin)
-- TODO
vim.api.nvim_exec_autocmds("BufEnter", {})
vim.api.nvim_exec_autocmds("BufEnter", {})
assert.spy(spy_load).called(1)
end)
it("Multiple events should only load plugin once", function()
---@param events LzEvent[]
local function itt(events)
---@type LzPlugin
local plugin = {
name = "foo",
event = events,
}
local spy_load = spy.on(loader, "_load")
state.plugins[plugin.name] = plugin
event.add(plugin)
vim.api.nvim_exec_autocmds(events[1].event, {
pattern = ".lua",
})
vim.api.nvim_exec_autocmds(events[2].event, {
pattern = ".lua",
})
assert.spy(spy_load).called(1)
end
itt({ event.parse("BufEnter"), event.parse("WinEnter") })
itt({ event.parse("WinEnter"), event.parse("BufEnter") })
end)
end)

0 comments on commit c17ba22

Please sign in to comment.