From be889f605030b29c093efb4963d66b38bd3bfe98 Mon Sep 17 00:00:00 2001 From: Bryce Dorn Date: Wed, 17 Oct 2018 15:47:16 -0500 Subject: [PATCH 1/3] convert targetingpairs tests to jest --- src/helpers/TargetingPairs.spec.jest.js | 239 +++++++++++++++++++++++ src/helpers/TargetingPairs.spec.js | 244 ------------------------ 2 files changed, 239 insertions(+), 244 deletions(-) create mode 100644 src/helpers/TargetingPairs.spec.jest.js delete mode 100644 src/helpers/TargetingPairs.spec.js diff --git a/src/helpers/TargetingPairs.spec.jest.js b/src/helpers/TargetingPairs.spec.jest.js new file mode 100644 index 0000000..04fbe09 --- /dev/null +++ b/src/helpers/TargetingPairs.spec.jest.js @@ -0,0 +1,239 @@ +var TargetingPairs = require('./TargetingPairs'); +var Experiments = require('./Experiments'); +var PageDepth = require('./PageDepth'); +var SocialReferrer = require('./SocialReferrer'); + +jest.mock('./Experiments'); +jest.mock('./PageDepth'); +jest.mock('./SocialReferrer'); + +describe('TargetingPairs', function() { + describe('#buildTargetingPairs', function() { + beforeEach(function() { + window.kinja = { + meta: { + blog: { name: 'Onion' }, + pageType: 'permalink' + }, + postMeta: {}, + categoryMeta: {}, + }; + window.Krux = { + segments: ['123', '456'], + user: 'foo' + } + }); + + describe('experiment is present', function() { + it('provides experiment info as targeting keys', function() { + Experiments.getExperimentVariation.mockReturnValueOnce('A'); + Experiments.getExperimentId.mockReturnValueOnce('12345'); + SocialReferrer.getSocialReferrer.mockResolvedValueOnce(''); + + const positionTargeting = 'top'; + + const windowStub = { + kinja: { + meta: { + blog: { name: 'Onion' }, + pageType: 'permalink' + }, + postMeta: {}, + categoryMeta: {}, + } + }; + + const targeting = TargetingPairs.buildTargetingPairs(windowStub, positionTargeting); + + expect(targeting.slotOptions.exp_variation).toEqual('12345_A_top'); + }); + }); + + describe('social referrer is present', function() { + it('provides the social referrer as slot option targeting', function() { + SocialReferrer.getSocialReferrer.mockReturnValueOnce('facebook'); + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.slotOptions.socialReferrer).toEqual('facebook'); + }); + }); + + describe('post id present', function() { + beforeEach(function() { + window.kinja.postMeta.postId = '1234'; + }); + + it('includes the post id in the slot option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.slotOptions.postId).toEqual('1234'); + }); + }); + + describe('page type', function() { + it('includes the page type in the slot option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.slotOptions.page).toEqual('permalink'); + }); + }); + + describe('page depth present', function() { + it('includes page depth in the slot option targeting', function() { + PageDepth.getPageDepth.mockReturnValueOnce(1); + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.slotOptions.pd).toEqual(1); + }); + }); + + describe('post tags are present', function() { + beforeEach(function() { + window.kinja.postMeta.tags = 'foo,bar,baz'; + }); + + it('includes tags in the page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.tags).toEqual(['foo', 'bar', 'baz']); + }); + }); + + describe('on the tag page', function() { + beforeEach(function() { + window.kinja.postMeta = {}; + window.kinja.tagMeta = { + tags: 'racing' + }; + }); + + it('includes tags in the page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.tags).toEqual(['racing']); + }); + }); + + describe('blog name', function() { + it('includes blog name in the page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.blogName).toEqual('Onion'); + }); + }); + + describe('category on post targeting', function() { + it('includes single category when on post meta', function() { + window.kinja.postMeta.categories = 'review'; + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.category).toEqual(['review']); + }); + + it('includes multiple categories when on post meta', function() { + window.kinja.postMeta.categories = 'review,cars'; + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.category).toEqual(['review', 'cars']); + }); + }); + + describe('category on category page', function() { + it('includes single category when on story type page', function() { + window.kinja.categoryMeta.categories = 'review'; + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.category).toEqual(['review']); + }); + + it('includes multiple categories when on category page', function() { + window.kinja.categoryMeta.categories = 'review,cars'; + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.category).toEqual(['review', 'cars']); + }); + }); + + + describe('Krux', function() { + beforeEach(function() { + window.Krux = { + segments: '123,456', + user: 'abc' + }; + }); + + describe('segments and user present', function() { + it('includes Krux segment in page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.ksg).toEqual('123,456'); + }); + + it('includes Krux kuid in page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.kuid).toEqual('abc'); + }); + }); + + describe('segments and user not present', function() { + beforeEach(function() { + window.Krux = {}; + }); + + it('does not include Krux segment in page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.ksg).toBe(undefined); + }); + + it('does not include Krux user in page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.kuid).toBe(undefined); + }); + }); + + describe('Krux not on the page', function() { + beforeEach(function() { + window.Krux = undefined; + }); + + it('does not include Krux segment in page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.ksg).toBe(undefined); + }); + + it('does not include Krux user in page option targeting', function() { + var targeting = TargetingPairs.buildTargetingPairs(window); + expect(targeting.pageOptions.kuid).toBe(undefined); + }); + }); + }); + }); + + describe('#getTargetingPairs', function() { + describe('without forced ad zone', function() { + it('sets forced ad zone as false', function() { + var pairs = TargetingPairs.getTargetingPairs(); + expect(pairs.pageOptions.forcedAdZone).toEqual(false); + }); + }); + + describe('with forced ad zone', function() { + it('uses forced ad zone', function() { + var pairs = TargetingPairs.getTargetingPairs('advertiser'); + expect(pairs.pageOptions.forcedAdZone).toEqual('advertiser'); + }); + }); + + describe('forced adzone exists but missing scope.kinja', function () { + beforeEach(function () { + delete window.kinja; + }); + + it('returns an object with a pageOptions.forcedAdZone property', function () { + var pairs = TargetingPairs.getTargetingPairs('advertiser'); + expect(pairs.pageOptions.forcedAdZone).toEqual('advertiser'); + }); + }); + + describe('missing scope.kinja', function() { + beforeEach(function() { + delete window.kinja; + }); + + it('returns an empty object and doesn\'t call buildTargetingPairs', function() { + var spy = jest.spyOn(TargetingPairs, 'buildTargetingPairs'); + expect(TargetingPairs.getTargetingPairs()).toEqual({}); + expect(spy).not.toHaveBeenCalled(); + }); + }); + }); +}); diff --git a/src/helpers/TargetingPairs.spec.js b/src/helpers/TargetingPairs.spec.js deleted file mode 100644 index 64306a6..0000000 --- a/src/helpers/TargetingPairs.spec.js +++ /dev/null @@ -1,244 +0,0 @@ -var Experiments = require('./Experiments'); -var SocialReferrer = require('./SocialReferrer'); -var PageDepth = require('./PageDepth'); - -describe('TargetingPairs', function() { - var TargetingPairs; - - beforeEach(function() { - TargetingPairs = require('./TargetingPairs'); - }); - - describe('#buildTargetingPairs', function() { - beforeEach(function() { - TestHelper.stub(Experiments, 'getExperimentVariation').returns('null'); - TestHelper.stub(Experiments, 'getExperimentId').returns('null'); - TestHelper.stub(SocialReferrer, 'getSocialReferrer').returns(''); - TestHelper.stub(PageDepth, 'getPageDepth').returns(1); - window.kinja = { - meta: { - blog: { name: 'Onion' }, - pageType: 'permalink' - }, - postMeta: {}, - categoryMeta: {}, - }; - window.Krux = { - segments: ['123', '456'], - user: 'foo' - } - }); - - describe('experiment is present', function() { - beforeEach(function() { - Experiments.getExperimentVariation.returns('A'); - Experiments.getExperimentId.returns('12345'); - }); - - it('provides experiment info as targeting keys', function() { - var positionTargeting = 'top', - targeting = TargetingPairs.buildTargetingPairs(window, positionTargeting); - expect(targeting.slotOptions.exp_variation).to.equal('12345_A_top'); - }); - }); - - describe('social referrer is present', function() { - beforeEach(function() { - SocialReferrer.getSocialReferrer.returns('facebook'); - }); - - it('provides the social referrer as slot option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.slotOptions.socialReferrer).to.equal('facebook'); - }); - }); - - describe('post id present', function() { - beforeEach(function() { - window.kinja.postMeta.postId = '1234'; - }); - - it('includes the post id in the slot option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.slotOptions.postId).to.equal('1234'); - }); - }); - - describe('page type', function() { - it('includes the page type in the slot option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.slotOptions.page).to.equal('permalink'); - }); - }); - - describe('page depth present', function() { - it('includes page depth in the slot option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.slotOptions.pd).to.equal(1); - }); - }); - - describe('post tags are present', function() { - beforeEach(function() { - window.kinja.postMeta.tags = 'foo,bar,baz'; - }); - - it('includes tags in the page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.tags).to.eql(['foo', 'bar', 'baz']); - }); - }); - - describe('on the tag page', function() { - beforeEach(function() { - window.kinja.postMeta = {}; - window.kinja.tagMeta = { - tags: 'racing' - }; - }); - - it('includes tags in the page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.tags).to.eql(['racing']); - }); - }); - - describe('blog name', function() { - it('includes blog name in the page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.blogName).to.equal('Onion'); - }); - }); - - describe('category on post targeting', function() { - it('includes single category when on post meta', function() { - window.kinja.postMeta.categories = 'review'; - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.category).to.eql(['review']); - }); - - it('includes multiple categories when on post meta', function() { - window.kinja.postMeta.categories = 'review,cars'; - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.category).to.eql(['review', 'cars']); - }); - }); - - describe('category on category page', function() { - it('includes single category when on story type page', function() { - window.kinja.categoryMeta.categories = 'review'; - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.category).to.eql(['review']); - }); - - it('includes multiple categories when on category page', function() { - window.kinja.categoryMeta.categories = 'review,cars'; - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.category).to.eql(['review', 'cars']); - }); - }); - - - describe('Krux', function() { - beforeEach(function() { - window.Krux = { - segments: '123,456', - user: 'abc' - }; - }); - - describe('segments and user present', function() { - it('includes Krux segment in page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.ksg).to.equal('123,456'); - }); - - it('includes Krux kuid in page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.kuid).to.equal('abc'); - }); - }); - - describe('segments and user not present', function() { - beforeEach(function() { - window.Krux = {}; - }); - - it('does not include Krux segment in page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.ksg).to.be.undefined; - }); - - it('does not include Krux user in page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.kuid).to.be.undefined; - }); - }); - - describe('Krux not on the page', function() { - beforeEach(function() { - window.Krux = undefined; - }); - - it('does not include Krux segment in page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.ksg).to.be.undefined; - }); - - it('does not include Krux user in page option targeting', function() { - var targeting = TargetingPairs.buildTargetingPairs(window); - expect(targeting.pageOptions.kuid).to.be.undefined; - }); - }); - }); - }); - - describe('#getTargetingPairs', function() { - beforeEach(function() { - TestHelper.stub(TargetingPairs, 'buildTargetingPairs').returns({ - pageOptions: { - blogName: 'Onion' - } - }); - }); - - context('without forced ad zone', function() { - it('sets forced ad zone as false', function() { - var pairs = TargetingPairs.getTargetingPairs(); - expect(pairs.pageOptions.forcedAdZone).to.be.false; - }); - }); - - context('with forced ad zone', function() { - it('uses forced ad zone', function() { - var pairs = TargetingPairs.getTargetingPairs('advertiser'); - expect(pairs.pageOptions.forcedAdZone).to.equal('advertiser'); - }); - }); - - context('forced adzone exists but missing scope.kinja', function () { - beforeEach(function () { - delete window.kinja; - }); - - it('returns an object with a pageOptions.forcedAdZone property', function () { - var pairs = TargetingPairs.getTargetingPairs('advertiser'); - expect(pairs.pageOptions.forcedAdZone).to.equal('advertiser'); - }); - }); - - context('missing scope.kinja', function() { - beforeEach(function() { - delete window.kinja - }); - - it('never tries to call buildTargetingPairs', function() { - expect(TargetingPairs.buildTargetingPairs.called).to.be.false; - }); - - it('returns an empty object', function() { - expect(TargetingPairs.getTargetingPairs()).to.eql({}); - }); - }); - }); -}); From d22d21f5af285f9681b4c43073a0a24c3e30af45 Mon Sep 17 00:00:00 2001 From: Bryce Dorn Date: Wed, 17 Oct 2018 15:52:35 -0500 Subject: [PATCH 2/3] convert utils spec to jest --- src/{utils.spec.js => utils.spec.jest.js} | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) rename src/{utils.spec.js => utils.spec.jest.js} (70%) diff --git a/src/utils.spec.js b/src/utils.spec.jest.js similarity index 70% rename from src/utils.spec.js rename to src/utils.spec.jest.js index 060bf24..d11978e 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.jest.js @@ -1,29 +1,25 @@ -describe('Utils', function () { - - var utils; - var element; +var utils = require('./utils'); +describe('Utils', function () { beforeEach(function () { - utils = require('./utils'); - - element = document.createElement('div'); + var element = document.createElement('div'); document.body.appendChild(element); }); it('should emit custom events', function () { var element = document.createElement('div'); - var spy = sinon.spy(); + var spy = jest.fn(); element.addEventListener('coolEvent', spy); utils.dispatchEvent(element, 'coolEvent'); - expect(spy).to.have.been.called; + expect(spy).toHaveBeenCalled(); }); - context('#extend', function() { + describe('#extend', function() { it('extends an object', function() { var extendedObject = { foo: '1', bar: '2' }; var objectWithOverrides = { foo: '3' }; - expect(utils.extend(extendedObject, objectWithOverrides).foo).to.equal('3'); + expect(utils.extend(extendedObject, objectWithOverrides).foo).toEqual('3'); }); }); }); From 4e6673deac8d9133fe76bcc6a4c2b66eb87af151 Mon Sep 17 00:00:00 2001 From: Bryce Dorn Date: Wed, 17 Oct 2018 16:02:03 -0500 Subject: [PATCH 3/3] maybe fix beforeEach issue --- src/helpers/TargetingPairs.spec.jest.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/TargetingPairs.spec.jest.js b/src/helpers/TargetingPairs.spec.jest.js index 04fbe09..88ed3a8 100644 --- a/src/helpers/TargetingPairs.spec.jest.js +++ b/src/helpers/TargetingPairs.spec.jest.js @@ -215,7 +215,7 @@ describe('TargetingPairs', function() { describe('forced adzone exists but missing scope.kinja', function () { beforeEach(function () { - delete window.kinja; + window.kinja = null; }); it('returns an object with a pageOptions.forcedAdZone property', function () { @@ -226,7 +226,7 @@ describe('TargetingPairs', function() { describe('missing scope.kinja', function() { beforeEach(function() { - delete window.kinja; + window.kinja = null; }); it('returns an empty object and doesn\'t call buildTargetingPairs', function() {