diff --git a/src/ad-cid.js b/src/ad-cid.js index 13190c6e9905..6f40efa78e0d 100644 --- a/src/ad-cid.js +++ b/src/ad-cid.js @@ -47,6 +47,7 @@ export function getOrCreateAdCid( } return cidService.get({ scope: dev().assertString(clientIdScope), + createCookieIfNotPresent: true, cookieName: opt_clientIdCookieName, }, Promise.resolve(undefined)).catch(error => { // Not getting a CID is not fatal. diff --git a/test/functional/test-ad-cid.js b/test/functional/test-ad-cid.js index efac361a775c..82b59d018123 100644 --- a/test/functional/test-ad-cid.js +++ b/test/functional/test-ad-cid.js @@ -16,16 +16,17 @@ import {adConfig} from '../../ads/_config'; import {ampdocServiceFor} from '../../src/ampdoc'; -import {createIframePromise} from '../../testing/iframe'; -import {cidServiceForDocForTesting,} from - '../../extensions/amp-analytics/0.1/cid-impl'; +import { + cidServiceForDocForTesting, +} from '../../extensions/amp-analytics/0.1/cid-impl'; import {installDocService} from '../../src/service/ampdoc-impl'; +import {installTimerService} from '../../src/service/timer-impl'; import {getAdCid} from '../../src/ad-cid'; -import {setCookie} from '../../src/cookies'; import {timerFor} from '../../src/services'; -import * as sinon from 'sinon'; +import {resetServiceForTesting} from '../../src/service'; +import * as lolex from 'lolex'; -describe('ad-cid', () => { +describes.realWin('ad-cid', {}, env => { const cidScope = 'cid-in-ads-test'; const config = adConfig['_ping_']; let sandbox; @@ -34,27 +35,25 @@ describe('ad-cid', () => { let clock; let element; let adElement; + let win; beforeEach(() => { - sandbox = sinon.sandbox.create(); - clock = sandbox.useFakeTimers(); - element = document.createElement('amp-ad'); + win = env.win; + sandbox = env.sandbox; + clock = lolex.install(win, 0, ['Date', 'setTimeout', 'clearTimeout']); + element = env.win.document.createElement('amp-ad'); element.setAttribute('type', '_ping_'); - installDocService(window, /* isSingleDoc */ true); - const ampdoc = ampdocServiceFor(window).getAmpDoc(); + installDocService(win, /* isSingleDoc */ true); + installTimerService(win); + const ampdoc = ampdocServiceFor(win).getAmpDoc(); cidService = cidServiceForDocForTesting(ampdoc); adElement = { getAmpDoc: () => ampdoc, element, - win: window, + win, }; }); - afterEach(() => { - sandbox.restore(); - setCookie(window, cidScope, '', Date.now() - 5000); - }); - it('should get correct cid', () => { config.clientIdScope = cidScope; @@ -67,6 +66,7 @@ describe('ad-cid', () => { expect(cid).to.equal('test123'); expect(getCidStruct).to.deep.equal({ scope: cidScope, + createCookieIfNotPresent: true, cookieName: undefined, }); }); @@ -85,6 +85,7 @@ describe('ad-cid', () => { expect(cid).to.equal('test123'); expect(getCidStruct).to.deep.equal({ scope: cidScope, + createCookieIfNotPresent: true, cookieName: 'different-cookie-name', }); }); @@ -93,11 +94,11 @@ describe('ad-cid', () => { it('should return on timeout', () => { config.clientIdScope = cidScope; sandbox.stub(cidService, 'get', () => { - return timerFor(window).promise(2000); + return timerFor(win).promise(2000); }); const p = getAdCid(adElement).then(cid => { expect(cid).to.be.undefined; - expect(Date.now()).to.equal(1000); + expect(win.Date.now()).to.equal(1000); }); clock.tick(999); // Let promises resolve before ticking 1 more ms. @@ -112,18 +113,12 @@ describe('ad-cid', () => { sandbox.stub(cidService, 'get', () => { return Promise.reject(new Error('nope')); }); - return getAdCid(adElement).then(cid => { - expect(cid).to.be.undefined; - }); + return expect(getAdCid(adElement)).to.eventually.be.undefined; }); it('should return null if cid service not available', () => { + resetServiceForTesting(win, 'cid'); config.clientIdScope = cidScope; - return createIframePromise(true /* runtimeOff */).then(iframe => { - adElement.win = iframe.win; - return getAdCid(adElement).then(cid => { - expect(cid).to.be.null; - }); - }); + return expect(getAdCid(adElement)).to.eventually.be.undefined; }); });