diff options
author | Stas Vilchik <vilchiks@gmail.com> | 2016-05-19 18:26:36 +0200 |
---|---|---|
committer | Stas Vilchik <vilchiks@gmail.com> | 2016-06-03 16:09:08 +0200 |
commit | 364cd42b9bb92eb42f0adce613185e0bb8771672 (patch) | |
tree | 4161fb41affa5ff3c6871959e302192e615116a9 /server/sonar-web/src/main/js/components/__tests__ | |
parent | 2ecead20c72c9348966fb7605e81df637b429e83 (diff) | |
download | sonarqube-364cd42b9bb92eb42f0adce613185e0bb8771672.tar.gz sonarqube-364cd42b9bb92eb42f0adce613185e0bb8771672.zip |
move tests
Diffstat (limited to 'server/sonar-web/src/main/js/components/__tests__')
-rw-r--r-- | server/sonar-web/src/main/js/components/__tests__/issue-test.js | 191 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/components/__tests__/source-viewer-test.js | 109 |
2 files changed, 300 insertions, 0 deletions
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 = '<span class="k">if</span> (<span class="sym-2 sym">a</span> + <span class="c">1</span>) {'; + const locations = [{ from: 1, to: 5 }]; + const result = helper(code, locations, 'x'); + expect(result).to.equal([ + '<span class="k">i</span>', + '<span class="k x">f</span>', + '<span class=" x"> (</span>', + '<span class="sym-2 sym x">a</span>', + '<span class=""> + </span>', + '<span class="c">1</span>', + '<span class="">) {</span>' + ].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([ + '<span class="">a</span>', + '<span class=" x">bcdef</span>', + '<span class="">ghijk</span>', + '<span class=" x">lmnop</span>', + '<span class="">qrst</span>' + ].join('')); + }); + + it('should mark one locations', function () { + const code = '<span class="cppd"> * Copyright (C) 2008-2014 SonarSource</span>'; + const locations = [{ from: 15, to: 20 }]; + const result = helper(code, locations, 'x'); + expect(result).to.equal([ + '<span class="cppd"> * Copyright (C</span>', + '<span class="cppd x">) 200</span>', + '<span class="cppd">8-2014 SonarSource</span>' + ].join('')); + }); + + it('should mark two locations', function () { + const code = '<span class="cppd"> * Copyright (C) 2008-2014 SonarSource</span>'; + const locations = [ + { from: 24, to: 29 }, + { from: 15, to: 20 } + ]; + const result = helper(code, locations, 'x'); + expect(result).to.equal([ + '<span class="cppd"> * Copyright (C</span>', + '<span class="cppd x">) 200</span>', + '<span class="cppd">8-20</span>', + '<span class="cppd x">14 So</span>', + '<span class="cppd">narSource</span>' + ].join('')); + }); + + it('should parse line with < and >', function () { + const code = '<span class="j">#include <stdio.h></span>'; + const result = helper(code, []); + expect(result).to.equal('<span class="j">#include <stdio.h></span>'); + }); + + it('should parse syntax and usage highlighting', function () { + const code = '<span class="k"><span class="sym-3 sym">this</span></span>'; + const expected = '<span class="k sym-3 sym">this</span>'; + const result = helper(code, []); + expect(result).to.equal(expected); + }); + + it('should parse nested tags', function () { + const code = '<span class="k"><span class="sym-3 sym">this</span> is</span>'; + const expected = '<span class="k sym-3 sym">this</span><span class="k"> is</span>'; + const result = helper(code, []); + expect(result).to.equal(expected); + }); + }); +}); + |