UNPKG

5.27 kBJavaScriptView Raw
1(function (Joi, request, reporter, helper) {
2 'use strict';
3
4 var expect = helper.chai.expect;
5
6 describe('Codacy Reporter', function () {
7 var bodyValidator,
8 sampleCoverageData;
9
10 beforeEach(function () {
11 bodyValidator = Joi.object({
12 total: Joi.number().valid(50),
13 fileReports: Joi.array().items(Joi.object({
14 filename: Joi.string().valid('filename.js'),
15 total: Joi.number().valid(10),
16 coverage: Joi.object({
17 1: Joi.number().valid(1),
18 2: Joi.number().valid(3)
19 })
20 }))
21 });
22
23 sampleCoverageData = {
24 total: 50,
25 fileReports: [
26 {
27 filename: 'filename.js',
28 total: 10,
29 coverage: {
30 1: 1,
31 2: 3
32 }
33 }
34 ]
35 };
36 });
37
38 it('should be able to use the mock end-point', function () {
39 return helper.setupMockEndpoint('1234', '4321', bodyValidator)
40 .then(function () {
41 return expect(request({
42 url: 'https://api.codacy.com/2.0/coverage/4321/javascript',
43 method: 'POST',
44 json: sampleCoverageData,
45 resolveWithFullResponse: true
46 }).promise()).to.eventually.satisfy(function (res) {
47 expect(res.statusCode).to.equal(200);
48 return true;
49 });
50 });
51 });
52 it('should be able to use the account api mock end-point', function() {
53 return helper.setupMockAccountApiEndpoint('1234', '4321', 'username', 'project-name', bodyValidator)
54 .then(function() {
55 return expect(request({
56 url: 'https://api.codacy.com/2.0/username/project-name/commit/4321/coverage/javascript',
57 method: 'POST',
58 json: sampleCoverageData,
59 resolveWithFullResponse: true
60 }).promise()).to.eventually.satisfy(function (res) {
61 expect(res.statusCode).to.equal(200);
62 return true;
63 });
64 });
65 });
66 it('shouldn\'t be able to create a reporter with invalid options', function () {
67 return expect(function () {
68 reporter({endpoint: 1});
69 }).to.throw(Error, 'child "endpoint" fails because ["endpoint" must be a string]');
70 });
71 it('should be able to use the reporter to send coverage data', function () {
72 return helper.setupMockEndpoint('1234', '4321', bodyValidator)
73 .then(function () {
74 return expect(reporter({})
75 .sendCoverage('1234', '4321', 'javascript', sampleCoverageData))
76 .to.eventually.be.fulfilled();
77 });
78 });
79 it('should receive error when non-200 status code', function () {
80 return helper.setupMockEndpoint('1234', '4321', bodyValidator, 204)
81 .then(function () {
82 return expect(reporter({})
83 .sendCoverage('1234', '4321', 'javascript', sampleCoverageData))
84 .to.eventually.be.rejectedWith(Error, 'Expected Status Code of 200, but got [204]');
85 });
86 });
87 it('should receive error when 400 level status code', function () {
88 return helper.setupMockEndpoint('1234', '4321', bodyValidator, 418)
89 .then(function () {
90 return expect(reporter({})
91 .sendCoverage('1234', '4321', 'javascript', sampleCoverageData))
92 .to.eventually.be.rejectedWith(Error, 'Expected Successful Status Code, but got [418]');
93 });
94 });
95 it('should be able to report data with an api token', function() {
96 return helper.setupMockAccountApiEndpoint('1234', '4321', 'username', 'project-name', bodyValidator)
97 .then(function() {
98 return expect(reporter({accountToken: '1234'})
99 .sendCoverage(null, '4321', 'javascript', sampleCoverageData, '1234', 'username', 'project-name'))
100 .to.eventually.be.fulfilled();
101 });
102 });
103 it('should not report data without a project name', function() {
104 return helper.setupMockAccountApiEndpoint('1234', '4321', 'username', 'project-name', bodyValidator)
105 .then(function() {
106 return expect(reporter({accountToken: '1234'})
107 .sendCoverage(null, '4321', 'javascript', sampleCoverageData, '1234', null, 'project-name'))
108 .to.eventually.be.rejected();
109 });
110 });
111 });
112
113}(require('joi'), require('request-promise'), require('../lib/reporter'), require('./helper')));