UNPKG

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