From 26aa8913bf38804f7bc4e7d8a362a14a8ab85dc5 Mon Sep 17 00:00:00 2001 From: "Douglas M." Date: Fri, 8 Mar 2024 16:14:45 +0100 Subject: [PATCH] tests: fix hanging test Use `assert.snapshot` to get rid of modifications made by the tests --- src/types/luassert.d.ts | 23 ++++++++-- tests/forem-nvim/setup_spec.lua | 60 ++++++++++++------------- tests/forem-nvim/setup_spec.ts | 80 +++++++++++++++------------------ 3 files changed, 84 insertions(+), 79 deletions(-) diff --git a/src/types/luassert.d.ts b/src/types/luassert.d.ts index 4d1aaa2..6d424d6 100644 --- a/src/types/luassert.d.ts +++ b/src/types/luassert.d.ts @@ -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; @@ -38,9 +46,16 @@ declare type SpyValue = Record; 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; diff --git a/tests/forem-nvim/setup_spec.lua b/tests/forem-nvim/setup_spec.lua index b747e8a..1a689c4 100644 --- a/tests/forem-nvim/setup_spec.lua +++ b/tests/forem-nvim/setup_spec.lua @@ -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 @@ -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 @@ -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 diff --git a/tests/forem-nvim/setup_spec.ts b/tests/forem-nvim/setup_spec.ts index 1c5d02d..d01bec5 100644 --- a/tests/forem-nvim/setup_spec.ts +++ b/tests/forem-nvim/setup_spec.ts @@ -1,86 +1,78 @@ -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) }); });