Skip to content

Commit 26aa891

Browse files
committed
tests: fix hanging test
Use `assert.snapshot` to get rid of modifications made by the tests
1 parent 813fa01 commit 26aa891

File tree

3 files changed

+84
-79
lines changed

3 files changed

+84
-79
lines changed

src/types/luassert.d.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
/**@noResolution*/
22
declare module "luassert.stub" {
3-
function stub(module: any): any;
3+
/**@noSelf*/
4+
interface Stub {
5+
new: (module: any, functionName: string) => any;
6+
}
7+
let exports: Stub
8+
export = exports
49
}
510

6-
declare type Stub = (module: any, functionName: string) => any;
7-
811
/**@noResolution*/
912
declare module "luassert.mock" {
10-
function mock(module: any): any;
13+
/**@noSelf*/
14+
interface Mock {
15+
new: (module: any, useStubs?: boolean) => any
16+
}
17+
let exports: Mock
18+
export = exports
1119
}
1220

1321
declare type Mock = (module: any, useStubs?: boolean) => any;
@@ -38,9 +46,16 @@ declare type SpyValue = Record<string, never>;
3846
declare namespace assert {
3947
export function stub(module: any): any;
4048
export function spy(spy: SpyValue): any;
49+
export function snapshot(this: any): Snapshot;
50+
export function is_true(value: boolean): void;
4151
const are: any;
4252
}
4353

54+
declare type Snapshot = {
55+
revert: (this: Snapshot) => void;
56+
};
57+
4458
declare function describe(description: string, suite: () => void): void;
4559
declare function before_each(fn: () => void): void;
60+
declare function after_each(fn: () => void): void;
4661
declare function it(description: string, test: () => void): void;

tests/forem-nvim/setup_spec.lua

+29-31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
22
local ____exports = {}
3+
local stub = require("luassert.stub")
34
local spy = require("luassert.spy")
45
local match = require("luassert.match")
5-
local stub = require("luassert.stub")
6-
local mock = require("luassert.mock")
76
local article = require("forem-nvim.article")
87
local function mockInternal(module)
98
_G.package.loaded["forem-nvim"] = nil
@@ -15,16 +14,21 @@ describe(
1514
"Forem.nvim",
1615
function()
1716
local foremNvim
17+
local snapshot
1818
before_each(function()
1919
vim.env.FOREM_API_KEY = "foo"
2020
_G.package.loaded["forem-nvim"] = nil
2121
foremNvim = require("forem-nvim")
22+
snapshot = assert:snapshot()
23+
end)
24+
after_each(function()
25+
snapshot:revert()
2226
end)
2327
it(
2428
"should show a notification when no api key is set",
2529
function()
2630
vim.env.FOREM_API_KEY = nil
27-
stub(vim, "notify")
31+
stub.new(vim, "notify")
2832
foremNvim.my_articles()
2933
assert.stub(vim.notify).was.called()
3034
end
@@ -38,41 +42,35 @@ describe(
3842
local foremNvimMocked = require("forem-nvim")
3943
foremNvimMocked.my_articles()
4044
assert.spy(mockedApi.myArticles).was.called()
41-
_G.package.loaded["forem-nvim.api"] = nil
4245
end
4346
)
4447
it(
4548
"should create a new article and open it",
4649
function()
47-
local ____vim_fn_0 = vim.fn
48-
local input = ____vim_fn_0.input
49-
vim.fn.input = spy.on(
50-
{input = function(_prompt) return "Title" end},
51-
"input"
52-
)
53-
stub(vim, "cmd")
54-
local mockedApi = mockInternal("forem-nvim.api")
55-
mockedApi.newArticle = spy.on(
56-
{newArticle = function(title) return {
57-
status = 201,
58-
body = {
59-
id = 1,
60-
body_markdown = article.getTemplate(title)
61-
}
62-
} end},
63-
"newArticle"
64-
)
65-
local mockedBuffer = mockInternal("forem-nvim.buffer")
66-
mockedBuffer.openMyArticle = spy.new(function()
67-
end)
68-
local foremNvimMocked = require("forem-nvim")
69-
foremNvimMocked.new_article()
70-
assert.spy(mockedBuffer.openMyArticle).was_called_with(match.is_same({
50+
local input = stub.new(vim.fn, "input")
51+
input.returns("Title")
52+
local api = mockInternal("forem-nvim.api")
53+
local apiNewArticle = stub.new(api, "newArticle")
54+
local newArticle = {
7155
id = 1,
7256
body_markdown = article.getTemplate("Title")
73-
}))
74-
vim.fn.input = input
75-
_G.package.loaded["forem-nvim.api"] = nil
57+
}
58+
apiNewArticle.returns({status = 201, body = newArticle})
59+
local buffer = mockInternal("forem-nvim.buffer")
60+
local bufferOpenMyArticle = spy.on(buffer, "openMyArticle")
61+
local foremNvimMocked = require("forem-nvim")
62+
foremNvimMocked.new_article()
63+
assert.stub(apiNewArticle).was.called_with("Title")
64+
assert.spy(bufferOpenMyArticle).was_called_with(match.is_same(newArticle))
65+
assert.are.same(
66+
"forem://my-article/" .. tostring(newArticle.id),
67+
vim.api.nvim_buf_get_name(0)
68+
)
69+
local bufferContent = vim.api.nvim_buf_get_lines(0, 0, -1, true)
70+
assert.are.same(
71+
article.getBodyLines(newArticle),
72+
bufferContent
73+
)
7674
end
7775
)
7876
end

tests/forem-nvim/setup_spec.ts

+36-44
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,78 @@
1-
const stub: Stub = require("luassert.stub");
2-
const mock: Mock = require("luassert.mock");
1+
import * as stub from "luassert.stub"
2+
import * as mock from "luassert.mock"
33
import * as spy from "luassert.spy";
44
import * as match from "luassert.match";
55
const article = require("forem-nvim.article");
66

77
const mockInternal = (module: string): any => {
8-
// To check if the api is called, we need to mock the api
9-
// The first step is to clear the api from the package.loaded table
108
_G.package.loaded["forem-nvim"] = undefined;
119
_G.package.loaded[module] = undefined;
1210

13-
// Then we mock the api
1411
const mocked = require(module);
1512
return mocked;
1613
};
1714

1815
describe("Forem.nvim", () => {
1916
let foremNvim: ForemNvim;
17+
let snapshot: Snapshot;
2018
before_each(() => {
2119
vim.env.FOREM_API_KEY = "foo";
2220
_G.package.loaded["forem-nvim"] = undefined;
2321
foremNvim = require("forem-nvim");
22+
snapshot = assert.snapshot();
23+
});
24+
25+
after_each(() => {
26+
snapshot.revert();
2427
});
2528

2629
it("should show a notification when no api key is set", () => {
2730
vim.env.FOREM_API_KEY = undefined;
28-
stub(vim, "notify");
31+
stub.new(vim, "notify");
2932
foremNvim.my_articles();
3033
assert.stub(vim.notify).was.called();
3134
});
3235

33-
it("should call the api to get the articles", () => {
34-
// To check if the api is called, we need to mock the api
35-
// The first step is to clear the api from the package.loaded table
36-
// _G.package.loaded["forem-nvim"] = undefined;
37-
// _G.package.loaded["forem-nvim.api"] = undefined;
38-
39-
// Then we mock the api
40-
// const mockedApi = require("forem-nvim.api");
41-
// We need to mock the function that we want to check
42-
// mockedApi.myArticles = spy.new(() => {});
43-
36+
it("should call the api to get the articles", function () {
4437
const mockedApi = mockInternal("forem-nvim.api");
4538
mockedApi.myArticles = spy.new(() => {});
46-
// Now we can require the package that will require the mocked api
39+
4740
const foremNvimMocked: ForemNvim = require("forem-nvim");
4841
foremNvimMocked.my_articles();
4942

50-
// Finally we can check if the function was called
5143
assert.spy(mockedApi.myArticles).was.called();
52-
53-
_G.package.loaded["forem-nvim.api"] = undefined;
5444
});
5545

56-
it("should create a new article and open it", () => {
57-
const { input } = vim.fn;
58-
vim.fn.input = spy.on({ input: (_prompt: string) => "Title" }, "input");
59-
stub(vim, "cmd");
46+
it("should create a new article and open it", function () {
47+
const input = stub.new(vim.fn, "input")
48+
input.returns("Title")
6049

61-
const mockedApi = mockInternal("forem-nvim.api");
62-
mockedApi.newArticle = spy.on(
63-
{
64-
newArticle: (title: string) => ({
65-
status: 201,
66-
body: { id: 1, body_markdown: article.getTemplate(title) },
67-
}),
68-
},
69-
"newArticle",
70-
);
71-
const mockedBuffer = mockInternal("forem-nvim.buffer");
72-
mockedBuffer.openMyArticle = spy.new(() => {});
50+
const api = mockInternal("forem-nvim.api");
51+
const apiNewArticle = stub.new(api, "newArticle")
52+
53+
const newArticle ={ id: 1, body_markdown: article.getTemplate("Title") }
54+
apiNewArticle.returns({
55+
status: 201,
56+
body: newArticle,
57+
});
58+
59+
const buffer = mockInternal("forem-nvim.buffer");
60+
const bufferOpenMyArticle = spy.on(buffer, "openMyArticle")
7361

7462
const foremNvimMocked: ForemNvim = require("forem-nvim");
7563
foremNvimMocked.new_article();
7664

77-
assert
78-
.spy(mockedBuffer.openMyArticle)
79-
.was_called_with(
80-
match.is_same({ id: 1, body_markdown: article.getTemplate("Title") }),
81-
);
65+
// Check if the API function was called correctly
66+
assert.stub(apiNewArticle).was.called_with("Title")
67+
68+
// Check if the buffer function was called correctly
69+
assert.spy(bufferOpenMyArticle).was_called_with(match.is_same(newArticle));
70+
71+
// Check if the buffer name is correct
72+
assert.are.same(`forem://my-article/${newArticle.id}`, vim.api.nvim_buf_get_name(0))
8273

83-
vim.fn.input = input;
84-
_G.package.loaded["forem-nvim.api"] = undefined;
74+
// Check if the buffer content is correct
75+
const bufferContent = vim.api.nvim_buf_get_lines(0, 0, -1, true)
76+
assert.are.same(article.getBodyLines(newArticle), bufferContent)
8577
});
8678
});

0 commit comments

Comments
 (0)