generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use `assert.snapshot` to get rid of modifications made by the tests
- Loading branch information
Showing
3 changed files
with
87 additions
and
79 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
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
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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |