extensions/amp-ad-network-doubleclick-impl/0.1/test/test-doubleclick-sra.js
/**
     * Tests SRA behavior by creating multiple doubleclick instances with the
     * following dimensions: networkId, number of instances, number of
     * invalid instances (meaning isValidElement returns false), and if SRA
     * XHR should fail.  Generates expected behaviors including XHR
     * requests, layoutCallback iframe state, and collapse.
     *
     * @param {!Array<number|{{
     *    networkId:number,
     *    instances:number,
     *    xhrFail:boolean|undefined,
     *    invalidInstances:number}}>} items
     */
Tests SRA behavior by creating multiple doubleclick instances with the
following dimensions: networkId, number of instances, number of
invalid instances (meaning isValidElement returns false), and if SRA
XHR should fail.  Generates expected behaviors including XHR
requests, layoutCallback iframe state, and collapse.

@param {!Array} items
Tests SRA behavior by creating multiple doubleclick instances with the following dimensions : networkId number of instances number of invalid instances ( meaning isValidElement returns false ) and if SRA XHR should fail . Generates expected behaviors including XHR requests layoutCallback iframe state and collapse . @param { !Array } items
function executeTest(items) {
      const networkXhrFailure = {};
      const networkValidity = {};
      const doubleclickInstances = [];
      const attemptCollapseSpy =
        sandbox.spy(BaseElement.prototype, 'attemptCollapse');
      let expectedAttemptCollapseCalls = 0;
      items.forEach(network => {
        if (typeof network == 'number') {
          network = {networkId: network, instances: 1};
        }
        dev().assert(network.instances || network.invalidInstances);
        const createInstances = (instanceCount, invalid) => {
          for (let i = 0; i < instanceCount; i++) {
            const impl = createA4aSraInstance(network.networkId);
            doubleclickInstances.push(impl);
            sandbox.stub(impl, 'isValidElement').returns(!invalid);
            if (invalid) {
              impl.element.setAttribute('data-test-invalid', 'true');
            }
          }
        };
        createInstances(network.instances);
        createInstances(network.invalidInstances, true);
        networkValidity[network.networkId] =
          network.invalidInstances && !network.instances;
        networkXhrFailure[network.networkId] = !!network.xhrFail;
        expectedAttemptCollapseCalls += network.xhrFail ? network.instances : 0;
      });
      const grouping = {};
      const groupingPromises = {};
      doubleclickInstances.forEach(impl => {
        const networkId = getNetworkId(impl.element);
        (grouping[networkId] || (grouping[networkId] = []))
            .push(impl);
        (groupingPromises[networkId] || (groupingPromises[networkId] = []))
            .push(Promise.resolve(impl));
      });
      sandbox.stub(AmpAdNetworkDoubleclickImpl.prototype, 'groupSlotsForSra')
          .returns(Promise.resolve(groupingPromises));
      let idx = 0;
      const layoutCallbacks = [];
      const getLayoutCallback = (impl, creative, isSra, noRender) => {
        impl.buildCallback();
        impl.onLayoutMeasure();
        return impl.layoutCallback().then(() => {
          if (noRender) {
            expect(impl.iframe).to.not.be.ok;
            return;
          }
          expect(impl.iframe).to.be.ok;
          const name = impl.iframe.getAttribute('name');
          if (isSra) {
            expect(name).to.match(
                new RegExp(`^\\d+-\\d+-\\d+;\\d+;${creative}`));
          } else {
            expect(JSON.parse(name).creative).to.equal(creative);
          }
        });
      };
      Object.keys(grouping).forEach(networkId => {
        const validInstances = grouping[networkId].filter(impl =>
          impl.element.getAttribute('data-test-invalid') != 'true');
        const isSra = validInstances.length > 1;
        const sraResponses = [];
        validInstances.forEach(impl => {
          const creative = `slot${idx++}`;
          if (isSra) {
            sraResponses.push({creative, headers: {slot: idx}});
          } else {
            generateNonSraXhrMockCall(impl, creative);
          }
          layoutCallbacks.push(getLayoutCallback(
              impl, creative, isSra,
              networkXhrFailure[networkId] ||
            impl.element.getAttribute('data-test-invalid') == 'true'));
        });
        if (isSra) {
          generateSraXhrMockCall(validInstances, networkId, sraResponses,
              networkXhrFailure[networkId], networkValidity[networkId]);
        }
      });
      return Promise.all(layoutCallbacks).then(() => expect(
          attemptCollapseSpy.callCount).to.equal(expectedAttemptCollapseCalls));
    }
function executeTest ( items ) { const networkXhrFailure = { } ; const networkValidity = { } ; const doubleclickInstances = [ ] ; const attemptCollapseSpy = sandbox . spy ( BaseElement . prototype , 'attemptCollapse' ) ; let expectedAttemptCollapseCalls = 0 ; items . forEach ( network => { if ( typeof network == 'number' ) { network = { networkId : network , instances : 1 } ; } dev ( ) . assert ( network . instances || network . invalidInstances ) ; const createInstances = ( instanceCount , invalid ) => { for ( let i = 0 ; i < instanceCount ; i ++ ) { const impl = createA4aSraInstance ( network . networkId ) ; doubleclickInstances . push ( impl ) ; sandbox . stub ( impl , 'isValidElement' ) . returns ( ! invalid ) ; if ( invalid ) { impl . element . setAttribute ( 'data-test-invalid' , 'true' ) ; } } } ; createInstances ( network . instances ) ; createInstances ( network . invalidInstances , true ) ; networkValidity [ network . networkId ] = network . invalidInstances && ! network . instances ; networkXhrFailure [ network . networkId ] = ! ! network . xhrFail ; expectedAttemptCollapseCalls += network . xhrFail ? network . instances : 0 ; } ) ; const grouping = { } ; const groupingPromises = { } ; doubleclickInstances . forEach ( impl => { const networkId = getNetworkId ( impl . element ) ; ( grouping [ networkId ] || ( grouping [ networkId ] = [ ] ) ) . push ( impl ) ; ( groupingPromises [ networkId ] || ( groupingPromises [ networkId ] = [ ] ) ) . push ( Promise . resolve ( impl ) ) ; } ) ; sandbox . stub ( AmpAdNetworkDoubleclickImpl . prototype , 'groupSlotsForSra' ) . returns ( Promise . resolve ( groupingPromises ) ) ; let idx = 0 ; const layoutCallbacks = [ ] ; const getLayoutCallback = ( impl , creative , isSra , noRender ) => { impl . buildCallback ( ) ; impl . onLayoutMeasure ( ) ; return impl . layoutCallback ( ) . then ( ( ) => { if ( noRender ) { expect ( impl . iframe ) . to . not . be . ok ; return ; } expect ( impl . iframe ) . to . be . ok ; const name = impl . iframe . getAttribute ( 'name' ) ; if ( isSra ) { expect ( name ) . to . match ( new RegExp ( ` \\ \\ \\ \\ ${ creative } ` ) ) ; } else { expect ( JSON . parse ( name ) . creative ) . to . equal ( creative ) ; } } ) ; } ; Object . keys ( grouping ) . forEach ( networkId => { const validInstances = grouping [ networkId ] . filter ( impl => impl . element . getAttribute ( 'data-test-invalid' ) != 'true' ) ; const isSra = validInstances . length > 1 ; const sraResponses = [ ] ; validInstances . forEach ( impl => { const creative = ` ${ idx ++ } ` ; if ( isSra ) { sraResponses . push ( { creative , headers : { slot : idx } } ) ; } else { generateNonSraXhrMockCall ( impl , creative ) ; } layoutCallbacks . push ( getLayoutCallback ( impl , creative , isSra , networkXhrFailure [ networkId ] || impl . element . getAttribute ( 'data-test-invalid' ) == 'true' ) ) ; } ) ; if ( isSra ) { generateSraXhrMockCall ( validInstances , networkId , sraResponses , networkXhrFailure [ networkId ] , networkValidity [ networkId ] ) ; } } ) ; return Promise . all ( layoutCallbacks ) . then ( ( ) => expect ( attemptCollapseSpy . callCount ) . to . equal ( expectedAttemptCollapseCalls ) ) ; }
Tests SRA behavior by creating multiple doubleclick instances with the
 following dimensions: networkId, number of instances, number of
 invalid instances (meaning isValidElement returns false), and if SRA
 XHR should fail.
Tests SRA behavior by creating multiple doubleclick instances with the following dimensions : networkId number of instances number of invalid instances ( meaning isValidElement returns false ) and if SRA XHR should fail .
// Store if XHR will fail by networkId. // Store if all elements for a given network are invalid. // Expect safeframe. // Expect nameframe render.