-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): add tests for throttle function
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |