Skip to content

Commit

Permalink
tests: fix hanging test
Browse files Browse the repository at this point in the history
Use `assert.snapshot` to get rid of modifications made by the tests
  • Loading branch information
Massolari committed Mar 8, 2024
1 parent 813fa01 commit 0e27e60
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 79 deletions.
23 changes: 19 additions & 4 deletions src/types/luassert.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
/**@noResolution*/
declare module "luassert.stub" {
function stub(module: any): any;
/**@noSelf*/
interface Stub {
new: (module: any, functionName: string) => any;
}
let exports: Stub
export = exports
}

declare type Stub = (module: any, functionName: string) => any;

/**@noResolution*/
declare module "luassert.mock" {
function mock(module: any): any;
/**@noSelf*/
interface Mock {
new: (module: any, useStubs?: boolean) => any
}
let exports: Mock
export = exports
}

declare type Mock = (module: any, useStubs?: boolean) => any;
Expand Down Expand Up @@ -38,9 +46,16 @@ declare type SpyValue = Record<string, never>;
declare namespace assert {
export function stub(module: any): any;
export function spy(spy: SpyValue): any;
export function snapshot(this: any): Snapshot;
export function is_true(value: boolean): void;
const are: any;
}

declare type Snapshot = {
revert: (this: Snapshot) => void;
};

declare function describe(description: string, suite: () => void): void;
declare function before_each(fn: () => void): void;
declare function after_each(fn: () => void): void;
declare function it(description: string, test: () => void): void;
60 changes: 29 additions & 31 deletions tests/forem-nvim/setup_spec.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
local ____exports = {}
local stub = require("luassert.stub")
local spy = require("luassert.spy")
local match = require("luassert.match")
local stub = require("luassert.stub")
local mock = require("luassert.mock")
local article = require("forem-nvim.article")
local function mockInternal(module)
_G.package.loaded["forem-nvim"] = nil
Expand All @@ -15,16 +14,21 @@ describe(
"Forem.nvim",
function()
local foremNvim
local snapshot
before_each(function()
vim.env.FOREM_API_KEY = "foo"
_G.package.loaded["forem-nvim"] = nil
foremNvim = require("forem-nvim")
snapshot = assert:snapshot()
end)
after_each(function()
snapshot:revert()
end)
it(
"should show a notification when no api key is set",
function()
vim.env.FOREM_API_KEY = nil
stub(vim, "notify")
stub.new(vim, "notify")
foremNvim.my_articles()
assert.stub(vim.notify).was.called()
end
Expand All @@ -38,41 +42,35 @@ describe(
local foremNvimMocked = require("forem-nvim")
foremNvimMocked.my_articles()
assert.spy(mockedApi.myArticles).was.called()
_G.package.loaded["forem-nvim.api"] = nil
end
)
it(
"should create a new article and open it",
function()
local ____vim_fn_0 = vim.fn
local input = ____vim_fn_0.input
vim.fn.input = spy.on(
{input = function(_prompt) return "Title" end},
"input"
)
stub(vim, "cmd")
local mockedApi = mockInternal("forem-nvim.api")
mockedApi.newArticle = spy.on(
{newArticle = function(title) return {
status = 201,
body = {
id = 1,
body_markdown = article.getTemplate(title)
}
} end},
"newArticle"
)
local mockedBuffer = mockInternal("forem-nvim.buffer")
mockedBuffer.openMyArticle = spy.new(function()
end)
local foremNvimMocked = require("forem-nvim")
foremNvimMocked.new_article()
assert.spy(mockedBuffer.openMyArticle).was_called_with(match.is_same({
local input = stub.new(vim.fn, "input")
input.returns("Title")
local api = mockInternal("forem-nvim.api")
local apiNewArticle = stub.new(api, "newArticle")
local newArticle = {
id = 1,
body_markdown = article.getTemplate("Title")
}))
vim.fn.input = input
_G.package.loaded["forem-nvim.api"] = nil
}
apiNewArticle.returns({status = 201, body = newArticle})
local buffer = mockInternal("forem-nvim.buffer")
local bufferOpenMyArticle = spy.on(buffer, "openMyArticle")
local foremNvimMocked = require("forem-nvim")
foremNvimMocked.new_article()
assert.stub(apiNewArticle).was.called_with("Title")
assert.spy(bufferOpenMyArticle).was_called_with(match.is_same(newArticle))
assert.are.same(
"forem://my-article/" .. tostring(newArticle.id),
vim.api.nvim_buf_get_name(0)
)
local bufferContent = vim.api.nvim_buf_get_lines(0, 0, -1, true)
assert.are.same(
article.getBodyLines(newArticle),
bufferContent
)
end
)
end
Expand Down
83 changes: 39 additions & 44 deletions tests/forem-nvim/setup_spec.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,81 @@
const stub: Stub = require("luassert.stub");
const mock: Mock = require("luassert.mock");
import * as stub from "luassert.stub";
import * as mock from "luassert.mock";
import * as spy from "luassert.spy";
import * as match from "luassert.match";
const article = require("forem-nvim.article");

const mockInternal = (module: string): any => {
// 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
_G.package.loaded["forem-nvim"] = undefined;
_G.package.loaded[module] = undefined;

// Then we mock the api
const mocked = require(module);
return mocked;
};

describe("Forem.nvim", () => {
let foremNvim: ForemNvim;
let snapshot: Snapshot;
before_each(() => {
vim.env.FOREM_API_KEY = "foo";
_G.package.loaded["forem-nvim"] = undefined;
foremNvim = require("forem-nvim");
snapshot = assert.snapshot();
});

after_each(() => {
snapshot.revert();
});

it("should show a notification when no api key is set", () => {
vim.env.FOREM_API_KEY = undefined;
stub(vim, "notify");
stub.new(vim, "notify");
foremNvim.my_articles();
assert.stub(vim.notify).was.called();
});

it("should call the api to get the articles", () => {
// 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
// _G.package.loaded["forem-nvim"] = undefined;
// _G.package.loaded["forem-nvim.api"] = undefined;

// Then we mock the api
// const mockedApi = require("forem-nvim.api");
// We need to mock the function that we want to check
// mockedApi.myArticles = spy.new(() => {});

it("should call the api to get the articles", function () {
const mockedApi = mockInternal("forem-nvim.api");
mockedApi.myArticles = spy.new(() => {});
// Now we can require the package that will require the mocked api

const foremNvimMocked: ForemNvim = require("forem-nvim");
foremNvimMocked.my_articles();

// Finally we can check if the function was called
assert.spy(mockedApi.myArticles).was.called();

_G.package.loaded["forem-nvim.api"] = undefined;
});

it("should create a new article and open it", () => {
const { input } = vim.fn;
vim.fn.input = spy.on({ input: (_prompt: string) => "Title" }, "input");
stub(vim, "cmd");
it("should create a new article and open it", function () {
const input = stub.new(vim.fn, "input");
input.returns("Title");

const mockedApi = mockInternal("forem-nvim.api");
mockedApi.newArticle = spy.on(
{
newArticle: (title: string) => ({
status: 201,
body: { id: 1, body_markdown: article.getTemplate(title) },
}),
},
"newArticle",
);
const mockedBuffer = mockInternal("forem-nvim.buffer");
mockedBuffer.openMyArticle = spy.new(() => {});
const api = mockInternal("forem-nvim.api");
const apiNewArticle = stub.new(api, "newArticle");

const newArticle = { id: 1, body_markdown: article.getTemplate("Title") };
apiNewArticle.returns({
status: 201,
body: newArticle,
});

const buffer = mockInternal("forem-nvim.buffer");
const bufferOpenMyArticle = spy.on(buffer, "openMyArticle");

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

assert
.spy(mockedBuffer.openMyArticle)
.was_called_with(
match.is_same({ id: 1, body_markdown: article.getTemplate("Title") }),
);
// Check if the API function was called correctly
assert.stub(apiNewArticle).was.called_with("Title");

// Check if the buffer function was called correctly
assert.spy(bufferOpenMyArticle).was_called_with(match.is_same(newArticle));

// Check if the buffer name is correct
assert.are.same(
`forem://my-article/${newArticle.id}`,
vim.api.nvim_buf_get_name(0),
);

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

0 comments on commit 0e27e60

Please sign in to comment.