From 364cd42b9bb92eb42f0adce613185e0bb8771672 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Thu, 19 May 2016 18:26:36 +0200 Subject: move tests --- .../src/main/js/components/__tests__/issue-test.js | 191 +++++++++++++++++++++ .../js/components/__tests__/source-viewer-test.js | 109 ++++++++++++ 2 files changed, 300 insertions(+) create mode 100644 server/sonar-web/src/main/js/components/__tests__/issue-test.js create mode 100644 server/sonar-web/src/main/js/components/__tests__/source-viewer-test.js (limited to 'server/sonar-web/src/main/js/components/__tests__') diff --git a/server/sonar-web/src/main/js/components/__tests__/issue-test.js b/server/sonar-web/src/main/js/components/__tests__/issue-test.js new file mode 100644 index 00000000000..75d4e89f119 --- /dev/null +++ b/server/sonar-web/src/main/js/components/__tests__/issue-test.js @@ -0,0 +1,191 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import chai, { expect } from 'chai'; +import sinon from 'sinon'; +import sinonChai from 'sinon-chai'; + +import Issue from '../issue/models/issue'; + +chai.use(sinonChai); + +describe('Issue', function () { + describe('Model', function () { + it('should have correct urlRoot', function () { + const issue = new Issue(); + expect(issue.urlRoot()).to.equal('/api/issues'); + }); + + it('should parse response without root issue object', function () { + const issue = new Issue(); + const example = { a: 1 }; + expect(issue.parse(example)).to.deep.equal(example); + }); + + it('should parse response with the root issue object', function () { + const issue = new Issue(); + const example = { a: 1 }; + expect(issue.parse({ issue: example })).to.deep.equal(example); + }); + + it('should reset attributes (no attributes initially)', function () { + const issue = new Issue(); + const example = { a: 1 }; + issue.reset(example); + expect(issue.toJSON()).to.deep.equal(example); + }); + + it('should reset attributes (override attribute)', function () { + const issue = new Issue({ a: 2 }); + const example = { a: 1 }; + issue.reset(example); + expect(issue.toJSON()).to.deep.equal(example); + }); + + it('should reset attributes (different attributes)', function () { + const issue = new Issue({ a: 2 }); + const example = { b: 1 }; + issue.reset(example); + expect(issue.toJSON()).to.deep.equal(example); + }); + + it('should unset `textRange` of a closed issue', function () { + const issue = new Issue(); + const result = issue.parse({ issue: { status: 'CLOSED', textRange: { startLine: 5 } } }); + expect(result.textRange).to.not.be.ok; + }); + + it('should unset `flows` of a closed issue', function () { + const issue = new Issue(); + const result = issue.parse({ issue: { status: 'CLOSED', flows: [1, 2, 3] } }); + expect(result.flows).to.deep.equal([]); + }); + + describe('Actions', function () { + it('should assign', function () { + const issue = new Issue({ key: 'issue-key' }); + const spy = sinon.spy(); + issue._action = spy; + issue.assign('admin'); + expect(spy).to.have.been.calledWith({ + data: { assignee: 'admin', issue: 'issue-key' }, + url: '/api/issues/assign' + }); + }); + + it('should unassign', function () { + const issue = new Issue({ key: 'issue-key' }); + const spy = sinon.spy(); + issue._action = spy; + issue.assign(); + expect(spy).to.have.been.calledWith({ + data: { assignee: undefined, issue: 'issue-key' }, + url: '/api/issues/assign' + }); + }); + + it('should plan', function () { + const issue = new Issue({ key: 'issue-key' }); + const spy = sinon.spy(); + issue._action = spy; + issue.plan('plan'); + expect(spy).to.have.been.calledWith({ data: { plan: 'plan', issue: 'issue-key' }, url: '/api/issues/plan' }); + }); + + it('should unplan', function () { + const issue = new Issue({ key: 'issue-key' }); + const spy = sinon.spy(); + issue._action = spy; + issue.plan(); + expect(spy).to.have.been.calledWith({ data: { plan: undefined, issue: 'issue-key' }, url: '/api/issues/plan' }); + }); + + it('should set severity', function () { + const issue = new Issue({ key: 'issue-key' }); + const spy = sinon.spy(); + issue._action = spy; + issue.setSeverity('BLOCKER'); + expect(spy).to.have.been.calledWith({ + data: { severity: 'BLOCKER', issue: 'issue-key' }, + url: '/api/issues/set_severity' + }); + }); + }); + + describe('#getLinearLocations', function () { + it('should return single line location', function () { + const issue = new Issue({ textRange: { startLine: 1, endLine: 1, startOffset: 0, endOffset: 10 } }); + const locations = issue.getLinearLocations(); + expect(locations.length).to.equal(1); + + expect(locations[0].line).to.equal(1); + expect(locations[0].from).to.equal(0); + expect(locations[0].to).to.equal(10); + }); + + it('should return location not from 0', function () { + const issue = new Issue({ textRange: { startLine: 1, endLine: 1, startOffset: 5, endOffset: 10 } }); + const locations = issue.getLinearLocations(); + expect(locations.length).to.equal(1); + + expect(locations[0].line).to.equal(1); + expect(locations[0].from).to.equal(5); + expect(locations[0].to).to.equal(10); + }); + + it('should return 2-lines location', function () { + const issue = new Issue({ textRange: { startLine: 2, endLine: 3, startOffset: 5, endOffset: 10 } }); + const locations = issue.getLinearLocations(); + expect(locations.length).to.equal(2); + + expect(locations[0].line).to.equal(2); + expect(locations[0].from).to.equal(5); + expect(locations[0].to).to.equal(999999); + + expect(locations[1].line).to.equal(3); + expect(locations[1].from).to.equal(0); + expect(locations[1].to).to.equal(10); + }); + + it('should return 3-lines location', function () { + const issue = new Issue({ textRange: { startLine: 4, endLine: 6, startOffset: 5, endOffset: 10 } }); + const locations = issue.getLinearLocations(); + expect(locations.length).to.equal(3); + + expect(locations[0].line).to.equal(4); + expect(locations[0].from).to.equal(5); + expect(locations[0].to).to.equal(999999); + + expect(locations[1].line).to.equal(5); + expect(locations[1].from).to.equal(0); + expect(locations[1].to).to.equal(999999); + + expect(locations[2].line).to.equal(6); + expect(locations[2].from).to.equal(0); + expect(locations[2].to).to.equal(10); + }); + + it('should return [] when no location', function () { + const issue = new Issue(); + const locations = issue.getLinearLocations(); + expect(locations.length).to.equal(0); + }); + }); + }); +}); diff --git a/server/sonar-web/src/main/js/components/__tests__/source-viewer-test.js b/server/sonar-web/src/main/js/components/__tests__/source-viewer-test.js new file mode 100644 index 00000000000..99ce9b0d700 --- /dev/null +++ b/server/sonar-web/src/main/js/components/__tests__/source-viewer-test.js @@ -0,0 +1,109 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { expect } from 'chai'; + +import helper from '../source-viewer/helpers/code-with-issue-locations-helper'; + +describe('Source Viewer', function () { + describe('Code With Issue Locations Helper', function () { + it('should be a function', function () { + expect(helper).to.be.a('function'); + }); + + it('should mark one location', function () { + const code = 'if (a + 1) {'; + const locations = [{ from: 1, to: 5 }]; + const result = helper(code, locations, 'x'); + expect(result).to.equal([ + 'i', + 'f', + ' (', + 'a', + ' + ', + '1', + ') {' + ].join('')); + }); + + it('should mark two locations', function () { + const code = 'abcdefghijklmnopqrst'; + const locations = [ + { from: 1, to: 6 }, + { from: 11, to: 16 } + ]; + const result = helper(code, locations, 'x'); + expect(result).to.equal([ + 'a', + 'bcdef', + 'ghijk', + 'lmnop', + 'qrst' + ].join('')); + }); + + it('should mark one locations', function () { + const code = ' * Copyright (C) 2008-2014 SonarSource'; + const locations = [{ from: 15, to: 20 }]; + const result = helper(code, locations, 'x'); + expect(result).to.equal([ + ' * Copyright (C', + ') 200', + '8-2014 SonarSource' + ].join('')); + }); + + it('should mark two locations', function () { + const code = ' * Copyright (C) 2008-2014 SonarSource'; + const locations = [ + { from: 24, to: 29 }, + { from: 15, to: 20 } + ]; + const result = helper(code, locations, 'x'); + expect(result).to.equal([ + ' * Copyright (C', + ') 200', + '8-20', + '14 So', + 'narSource' + ].join('')); + }); + + it('should parse line with < and >', function () { + const code = '#include <stdio.h>'; + const result = helper(code, []); + expect(result).to.equal('#include <stdio.h>'); + }); + + it('should parse syntax and usage highlighting', function () { + const code = 'this'; + const expected = 'this'; + const result = helper(code, []); + expect(result).to.equal(expected); + }); + + it('should parse nested tags', function () { + const code = 'this is'; + const expected = 'this is'; + const result = helper(code, []); + expect(result).to.equal(expected); + }); + }); +}); + -- cgit v1.2.3