From 139ff6b80c087bb24525ae32411876f46619cc06 Mon Sep 17 00:00:00 2001 From: Tomas Vik Date: Mon, 1 Mar 2021 11:02:55 +0100 Subject: [PATCH] test(mr review): add integration tests for initializing MR discussions --- .../items/mr_item_model.test.ts | 2 +- .../fixtures/graphql/discussions.js | 22 +++++++-- test/integration/mr_review.test.js | 45 +++++++++++++++++++ test/integration/webview.test.js | 4 +- 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/data_providers/items/mr_item_model.test.ts b/src/data_providers/items/mr_item_model.test.ts index 1c1d84f..c97f8ca 100644 --- a/src/data_providers/items/mr_item_model.test.ts +++ b/src/data_providers/items/mr_item_model.test.ts @@ -43,7 +43,7 @@ describe('MrItemModel', () => { const [uri, range] = createCommentThreadMock.mock.calls[0]; expect(uri.path).toBe('src/webview/src/components/LabelNote.vue'); expect(range.start.x).toBe(47); - expect(commentThread.comments.length).toBe(2); + expect(commentThread.comments.length).toBe(1); const firstComment = commentThread.comments[0]; expect(firstComment.author.name).toBe('Tomas Vik'); expect(firstComment.mode).toBe(vscode.CommentMode.Preview); diff --git a/test/integration/fixtures/graphql/discussions.js b/test/integration/fixtures/graphql/discussions.js index 89b0f78..f6cd55d 100644 --- a/test/integration/fixtures/graphql/discussions.js +++ b/test/integration/fixtures/graphql/discussions.js @@ -132,11 +132,11 @@ const discussionOnDiff = { hasNextPage: false, endCursor: 'MQ', }, - nodes: [noteOnDiff, note2], + nodes: [noteOnDiff], }, }; -const projectWithDiscussions = { +const projectWithIssueDiscussions = { project: { id: 'gid://gitlab/Project/278964', issue: { @@ -151,8 +151,24 @@ const projectWithDiscussions = { }, }; +const projectWithMrDiscussions = { + project: { + id: 'gid://gitlab/Project/278964', + mergeRequest: { + discussions: { + pageInfo: { + hasNextPage: false, + endCursor: 'Nw', + }, + nodes: [systemNote, singleNote, discussionOnDiff], + }, + }, + }, +}; + module.exports = { - projectWithDiscussions, + projectWithIssueDiscussions, + projectWithMrDiscussions, note1, note1TextSnippet, note2, diff --git a/test/integration/mr_review.test.js b/test/integration/mr_review.test.js index 5e7e5e6..ab8fc76 100644 --- a/test/integration/mr_review.test.js +++ b/test/integration/mr_review.test.js @@ -1,10 +1,15 @@ const assert = require('assert'); +const sinon = require('sinon'); +const vscode = require('vscode'); +const { graphql } = require('msw'); + const IssuableDataProvider = require('../../src/data_providers/issuable').DataProvider; const { MrItemModel } = require('../../src/data_providers/items/mr_item_model'); const { tokenService } = require('../../src/services/token_service'); const openMergeRequestResponse = require('./fixtures/rest/open_mr.json'); const versionsResponse = require('./fixtures/rest/versions.json'); const versionResponse = require('./fixtures/rest/mr_version.json'); +const { projectWithMrDiscussions, noteOnDiff } = require('./fixtures/graphql/discussions'); const { getServer, createJsonEndpoint, @@ -31,6 +36,11 @@ describe('MR Review', () => { '?ref=1f0fa02de1f6b913d674a8be10899fb8540237a9': 'Old Version', '?ref=b6d6f6fd17b52b8cf4e961218c572805e9aa7463': 'New Version', }), + graphql.query('GetMrDiscussions', (req, res, ctx) => { + if (req.variables.projectPath === 'gitlab-org/gitlab' && req.variables.iid === '33824') + return res(ctx.data(projectWithMrDiscussions)); + return res(ctx.data({ project: null })); + }), ]); await tokenService.setToken(GITLAB_URL, 'abcd-secret'); }); @@ -73,6 +83,41 @@ describe('MR Review', () => { ); }); + describe('discussions', () => { + const sandbox = sinon.createSandbox(); + let thread; + let commentController; + + beforeEach(() => { + thread = {}; + /* We fake createCommentController implementation to check + that when we initialize an MR review, we create a correct comment controller + we save the created thread for later use in assertions */ + commentController = { + createCommentThread: (uri, range, comments) => { + thread = { uri, range, comments }; + return thread; + }, + }; + sandbox.stub(vscode.comments, 'createCommentController').returns(commentController); + }); + afterEach(() => { + sandbox.restore(); + }); + + it('loads MR discussions', async () => { + const mrItem = getTreeItem(mrItemModel); + assert.strictEqual(mrItem.label, '!33824 ยท Web IDE - remove unused actions (mappings)'); + + await dataProvider.getChildren(mrItemModel); + + const { uri, range, comments } = thread; + assert.strictEqual(uri.path, `/${noteOnDiff.position.newPath}`); + assert.strictEqual(range.start.line, noteOnDiff.position.oldLine - 1); + assert.strictEqual(comments[0].body, noteOnDiff.body); + }); + }); + describe('clicking on a changed file', () => { let mrFiles; diff --git a/test/integration/webview.test.js b/test/integration/webview.test.js index 9ac8f92..744f659 100644 --- a/test/integration/webview.test.js +++ b/test/integration/webview.test.js @@ -6,7 +6,7 @@ const { graphql } = require('msw'); const webviewController = require('../../src/webview_controller'); const { tokenService } = require('../../src/services/token_service'); const openIssueResponse = require('./fixtures/rest/open_issue.json'); -const { projectWithDiscussions } = require('./fixtures/graphql/discussions'); +const { projectWithIssueDiscussions } = require('./fixtures/graphql/discussions'); const { getServer, createJsonEndpoint } = require('./test_infrastructure/mock_server'); const { GITLAB_URL } = require('./test_infrastructure/constants'); @@ -29,7 +29,7 @@ describe('GitLab webview', () => { server = getServer([ graphql.query('GetIssueDiscussions', (req, res, ctx) => { if (req.variables.projectPath === 'gitlab-org/gitlab') - return res(ctx.data(projectWithDiscussions)); + return res(ctx.data(projectWithIssueDiscussions)); return res(ctx.data({ project: null })); }), graphql.mutation('CreateNote', (req, res, ctx) => { -- GitLab