|
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" |
3 | 3 | import * as spy from "luassert.spy";
|
4 | 4 | import * as match from "luassert.match";
|
5 | 5 | const article = require("forem-nvim.article");
|
6 | 6 |
|
7 | 7 | 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 |
10 | 8 | _G.package.loaded["forem-nvim"] = undefined;
|
11 | 9 | _G.package.loaded[module] = undefined;
|
12 | 10 |
|
13 |
| - // Then we mock the api |
14 | 11 | const mocked = require(module);
|
15 | 12 | return mocked;
|
16 | 13 | };
|
17 | 14 |
|
18 | 15 | describe("Forem.nvim", () => {
|
19 | 16 | let foremNvim: ForemNvim;
|
| 17 | + let snapshot: Snapshot; |
20 | 18 | before_each(() => {
|
21 | 19 | vim.env.FOREM_API_KEY = "foo";
|
22 | 20 | _G.package.loaded["forem-nvim"] = undefined;
|
23 | 21 | foremNvim = require("forem-nvim");
|
| 22 | + snapshot = assert.snapshot(); |
| 23 | + }); |
| 24 | + |
| 25 | + after_each(() => { |
| 26 | + snapshot.revert(); |
24 | 27 | });
|
25 | 28 |
|
26 | 29 | it("should show a notification when no api key is set", () => {
|
27 | 30 | vim.env.FOREM_API_KEY = undefined;
|
28 |
| - stub(vim, "notify"); |
| 31 | + stub.new(vim, "notify"); |
29 | 32 | foremNvim.my_articles();
|
30 | 33 | assert.stub(vim.notify).was.called();
|
31 | 34 | });
|
32 | 35 |
|
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 () { |
44 | 37 | const mockedApi = mockInternal("forem-nvim.api");
|
45 | 38 | mockedApi.myArticles = spy.new(() => {});
|
46 |
| - // Now we can require the package that will require the mocked api |
| 39 | + |
47 | 40 | const foremNvimMocked: ForemNvim = require("forem-nvim");
|
48 | 41 | foremNvimMocked.my_articles();
|
49 | 42 |
|
50 |
| - // Finally we can check if the function was called |
51 | 43 | assert.spy(mockedApi.myArticles).was.called();
|
52 |
| - |
53 |
| - _G.package.loaded["forem-nvim.api"] = undefined; |
54 | 44 | });
|
55 | 45 |
|
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") |
60 | 49 |
|
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") |
73 | 61 |
|
74 | 62 | const foremNvimMocked: ForemNvim = require("forem-nvim");
|
75 | 63 | foremNvimMocked.new_article();
|
76 | 64 |
|
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)) |
82 | 73 |
|
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) |
85 | 77 | });
|
86 | 78 | });
|
0 commit comments