UNPKG

3.9 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'),
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',
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('shouldn\'t be able to send coverage with 0 hits on a line', function () {
53 sampleCoverageData.fileReports[0].coverage['3'] = 0;
54
55 return expect(reporter({})
56 .sendCoverage('1234', '4321', sampleCoverageData))
57 .to.eventually.be.rejectedWith(Error, 'child "fileReports" fails because ["fileReports" does not contain 1 required value(s)]');
58 });
59 it('shouldn\'t be able to create a reporter with invalid options', function () {
60 return expect(function () {
61 reporter({endpoint: 1});
62 }).to.throw(Error, 'child "endpoint" fails because ["endpoint" must be a string]');
63 });
64 it('should be able to use the reporter to send coverage data', function () {
65 return helper.setupMockEndpoint('1234', '4321', bodyValidator)
66 .then(function () {
67 return expect(reporter({})
68 .sendCoverage('1234', '4321', sampleCoverageData))
69 .to.eventually.be.fulfilled();
70 });
71 });
72 it('should receive error when non-200 status code', function () {
73 return helper.setupMockEndpoint('1234', '4321', bodyValidator, 204)
74 .then(function () {
75 return expect(reporter({})
76 .sendCoverage('1234', '4321', sampleCoverageData))
77 .to.eventually.be.rejectedWith(Error, 'Expected Status Code of 200, but got [204]');
78 });
79 });
80 it('should receive error when 400 level status code', function () {
81 return helper.setupMockEndpoint('1234', '4321', bodyValidator, 418)
82 .then(function () {
83 return expect(reporter({})
84 .sendCoverage('1234', '4321', sampleCoverageData))
85 .to.eventually.be.rejectedWith(Error, 'Expected Successful Status Code, but got [418]');
86 });
87 });
88 });
89
90}(require('joi'), require('request-promise'), require('../lib/reporter'), require('./helper')));