|
| 1 | +const { expect } = require("chai"); |
| 2 | +const chai = require("chai"), |
| 3 | + chaiAsPromised = require("chai-as-promised"), |
| 4 | + sinon = require('sinon'), |
| 5 | + request = require('request'); |
| 6 | + |
| 7 | +const logger = require("../../../../bin/helpers/logger").winstonLogger, |
| 8 | + testObjects = require("../../support/fixtures/testObjects"), |
| 9 | + utils = require('../../../../bin/helpers/utils'), |
| 10 | + atsHelper = require('../../../../bin/helpers/atsHelper'); |
| 11 | + |
| 12 | +chai.use(chaiAsPromised); |
| 13 | +logger.transports["console.info"].silent = true; |
| 14 | + |
| 15 | +describe('#atsHelper', () => { |
| 16 | + let bsConfig = testObjects.sampleBsConfig; |
| 17 | + let sendUsageReportStub = null, requestStub = null; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + sendUsageReportStub = sinon.stub(utils, 'sendUsageReport'); |
| 21 | + requestStub = sinon.stub(request, "get"); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + sinon.restore(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('#isTurboScaleSession', () => { |
| 29 | + it('return true if env var set to true', () => { |
| 30 | + process.env.BROWSERSTACK_TURBOSCALE = "true"; |
| 31 | + expect(atsHelper.isTurboScaleSession(bsConfig)).to.be.true; |
| 32 | + delete BROWSERSTACK_TURBOSCALE; |
| 33 | + }); |
| 34 | + |
| 35 | + it('return false if env var set to incorrect value', () => { |
| 36 | + process.env.BROWSERSTACK_TURBOSCALE = "false"; |
| 37 | + expect(atsHelper.isTurboScaleSession(bsConfig)).to.be.false; |
| 38 | + delete BROWSERSTACK_TURBOSCALE; |
| 39 | + }); |
| 40 | + |
| 41 | + it('return true if set in config', () => { |
| 42 | + expect(atsHelper.isTurboScaleSession(testObjects.sampleATSBsConfig)).to.be.true; |
| 43 | + }); |
| 44 | + |
| 45 | + it('return false if not set in config as well as env var', () => { |
| 46 | + expect(atsHelper.isTurboScaleSession(bsConfig)).to.be.false; |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('#getTurboScaleOptions', () => { |
| 51 | + it('return empty object if not set', () => { |
| 52 | + expect(atsHelper.getTurboScaleOptions(testObjects.sampleATSBsConfig)).to.deep.include({}); |
| 53 | + }); |
| 54 | + |
| 55 | + it('return obj if set in config', () => { |
| 56 | + expect(atsHelper.getTurboScaleOptions(testObjects.sampleATSBsConfigWithOptions)).to.deep.include({ gridName: 'abc' }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('#getTurboScaleGridName', () => { |
| 61 | + it('return value set by env var', () => { |
| 62 | + process.env.BROWSERSTACK_TURBOSCALE_GRID_NAME = "my-grid"; |
| 63 | + expect(atsHelper.getTurboScaleGridName(testObjects.sampleATSBsConfigWithOptions)).to.eq('my-grid'); |
| 64 | + delete process.env.BROWSERSTACK_TURBOSCALE_GRID_NAME; |
| 65 | + }); |
| 66 | + |
| 67 | + it('return value set in config', () => { |
| 68 | + expect(atsHelper.getTurboScaleGridName(testObjects.sampleATSBsConfigWithOptions)).to.eq('abc'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('return NO_GRID_NAME_PASSED if value not set in config as well as env var', () => { |
| 72 | + expect(atsHelper.getTurboScaleGridName(bsConfig)).to.eq('NO_GRID_NAME_PASSED'); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('#getTurboScaleGridDetails', () => { |
| 77 | + it('resolve with body', () => { |
| 78 | + const body = { |
| 79 | + id: '123', |
| 80 | + name: 'grid-name', |
| 81 | + url: 'https://abc.com/wd/hub', |
| 82 | + playwrightUrl: 'wss://abc.com/playwright', |
| 83 | + cypressUrl: 'https://abc.com/cypress', |
| 84 | + isTrialGrid: false, |
| 85 | + customRepeaters: '' |
| 86 | + }; |
| 87 | + |
| 88 | + requestStub.yields(null, { statusCode: 200 }, body); |
| 89 | + sendUsageReportStub.notCalled; |
| 90 | + atsHelper.getTurboScaleGridDetails(testObjects.sampleATSBsConfigWithOptions).then((result) => { |
| 91 | + expect(result).to.eq(body); |
| 92 | + }) |
| 93 | + }); |
| 94 | + |
| 95 | + it('reject with empty object', () => { |
| 96 | + const body = { |
| 97 | + id: '123', |
| 98 | + name: 'grid-name', |
| 99 | + url: 'https://abc.com/wd/hub', |
| 100 | + playwrightUrl: 'wss://abc.com/playwright', |
| 101 | + cypressUrl: 'https://abc.com/cypress', |
| 102 | + isTrialGrid: false, |
| 103 | + customRepeaters: '' |
| 104 | + }; |
| 105 | + |
| 106 | + requestStub.yields(null, { statusCode: 422 }, body); |
| 107 | + atsHelper.getTurboScaleGridDetails(testObjects.sampleATSBsConfigWithOptions).then((result) => { |
| 108 | + expect(result).to.eq({}); |
| 109 | + expect(sendUsageReportStub.calledOnce).to.be.true; |
| 110 | + }) |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments