Skip to content

Commit

Permalink
feat(test): add tests for throttle function
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Jun 26, 2024
1 parent 7314f3e commit 1287db8
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions spec/utils/async_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---@diagnostic disable: need-check-nil
local async = require("neocomplete.utils.async")

describe("Throttle", function()
local count
before_each(function()
count = 0
end)
local test_func = function()
count = count + 1
end
it("makes first call immediately", function()
local start = vim.uv.now()
local fn = async.throttle(test_func, 100)
fn()
vim.wait(1000, function()
return count == 1
end)
local ms_passed = vim.uv.now() - start
assert.is.truthy(ms_passed < 30)
end)

it("waits for timeout before second call", function()
local start = vim.uv.now()
local fn = async.throttle(test_func, 100)
fn()
fn()
vim.wait(1000, function()
return count == 2
end)
local ms_passed = vim.uv.now() - start
assert.is.truthy(ms_passed > 100 and ms_passed < 200)
end)

it("doesn't queue up more than one call", function()
local start = vim.uv.now()
local fn = async.throttle(test_func, 100)
fn()
fn()
fn()
vim.wait(1000, function()
return count == 3
end)
local ms_passed = vim.uv.now() - start
assert.is.truthy(ms_passed > 1000)
end)
end)

0 comments on commit 1287db8

Please sign in to comment.