Merge branch 'api-v3' into api-v3-i18n
This commit is contained in:
@@ -5,10 +5,10 @@ import {
|
||||
} from '../../../../helpers/api-unit.helper';
|
||||
import analyticsService from '../../../../../website/src/libs/api-v3/analyticsService'
|
||||
import nconf from 'nconf';
|
||||
import attachAnalytics from '../../../../../website/src/middlewares/api-v3/analytics';
|
||||
|
||||
describe('analytics middleware', function() {
|
||||
let res, req, next;
|
||||
let pathToAnalyticsMiddleware = '../../../../../website/src/middlewares/api-v3/analytics';
|
||||
|
||||
beforeEach(() => {
|
||||
res = generateRes();
|
||||
@@ -16,17 +16,40 @@ describe('analytics middleware', function() {
|
||||
next = generateNext();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// The nconf.get('IS_PROD') occurs when the file is required
|
||||
// Since node caches IS_PROD, we have to delete it from the cache
|
||||
// to test prod vs non-prod behaviors
|
||||
delete require.cache[require.resolve(pathToAnalyticsMiddleware)];
|
||||
});
|
||||
|
||||
it('attaches analytics object res.locals', function() {
|
||||
let attachAnalytics = require(pathToAnalyticsMiddleware);
|
||||
|
||||
attachAnalytics(req, res, next);
|
||||
|
||||
expect(res.analytics).to.exist;
|
||||
});
|
||||
|
||||
it('attaches stubbed methods for non-prod environments', () => {
|
||||
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(false);
|
||||
let attachAnalytics = require(pathToAnalyticsMiddleware);
|
||||
|
||||
attachAnalytics(req, res, next);
|
||||
|
||||
expect(res.analytics.track).to.eql(analyticsService.mockAnalyticsService.track);
|
||||
expect(res.analytics.trackPurchase).to.eql(analyticsService.mockAnalyticsService.trackPurchase);
|
||||
});
|
||||
|
||||
it('attaches real methods for prod environments', () => {
|
||||
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true);
|
||||
|
||||
let attachAnalytics = require(pathToAnalyticsMiddleware);
|
||||
|
||||
attachAnalytics(req, res, next);
|
||||
|
||||
expect(res.analytics.track).to.eql(analyticsService.track);
|
||||
expect(res.analytics.trackPurchase).to.eql(analyticsService.trackPurchase);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user