Skip to content

Commit

Permalink
Write cookie for ad-cid. (ampproject#9594)
Browse files Browse the repository at this point in the history
* Write cookie for ad-cid.

* fix tests
  • Loading branch information
lannka authored and Aaron Turner committed Jun 6, 2017
1 parent c941ccd commit dcf76e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/ad-cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 23 additions & 28 deletions test/functional/test-ad-cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -67,6 +66,7 @@ describe('ad-cid', () => {
expect(cid).to.equal('test123');
expect(getCidStruct).to.deep.equal({
scope: cidScope,
createCookieIfNotPresent: true,
cookieName: undefined,
});
});
Expand All @@ -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',
});
});
Expand All @@ -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.
Expand All @@ -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;
});
});

0 comments on commit dcf76e5

Please sign in to comment.