Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimonBerlin committed Oct 9, 2018
1 parent be67242 commit 0bfd2d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/scripts/core/core_vendor_information.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ export let cachedVendorList;
export let pendingVendorlistPromise = null;

export function loadVendorList() {
if (pendingVendorlistPromise) {
return pendingVendorlistPromise;
} else if (cachedVendorList) {
return new Promise(function (resolve) {
if (cachedVendorList) {
return new Promise( resolve => {
resolve(cachedVendorList);
});
} else if (pendingVendorlistPromise) {
return pendingVendorlistPromise;
} else {
return new Promise(function (resolve) {
let iabVendorListUrl = getIabVendorListUrl();
pendingVendorlistPromise = fetchJsonData(iabVendorListUrl)
pendingVendorlistPromise.then(response => {
sortVendors(response);
cachedVendorList = response;
pendingVendorlistPromise = null;
sortVendors(cachedVendorList);
resolve(cachedVendorList);
})
.catch(error => {
Expand Down
23 changes: 16 additions & 7 deletions test/specs/core/core_vendor_information.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ describe('core_vendor_information', () => {
it('should use cached vendor list if there is one', (done) => {
let fetchSpy = spyOn(CoreUtils, 'fetchJsonData').and.returnValue(new Promise((resolve) => resolve(VENDOR_LIST)));
spyOn(CoreConfig, 'getIabVendorListUrl').and.returnValue("https://iab.vendor.list.url");

expect(pendingVendorlistPromise).toBeNull();
expect(cachedVendorList).toBeUndefined();

loadVendorList().then(() => {
expect(CoreUtils.fetchJsonData).toHaveBeenCalled();
fetchSpy.calls.reset();
expect(pendingVendorlistPromise).toBeNull();
expect(cachedVendorList).toBeDefined();

loadVendorList().then(() => {
expect(CoreUtils.fetchJsonData).not.toHaveBeenCalled();
Expand All @@ -55,25 +60,29 @@ describe('core_vendor_information', () => {
});

it('should wait for cached vendor list if request is already started', (done) => {
let fetchSpy = spyOn(CoreUtils, 'fetchJsonData').and.returnValue(new Promise((resolve) => resolve(VENDOR_LIST)));
let fetchSpy = spyOn(CoreUtils, 'fetchJsonData').and.returnValue(
new Promise( resolve => {
setTimeout( () => { resolve(VENDOR_LIST) } , 2000 );
}));
spyOn(CoreConfig, 'getIabVendorListUrl').and.returnValue("https://iab.vendor.list.url");

expect(pendingVendorlistPromise).toBeNull();
expect(cachedVendorList).toBeUndefined();

loadVendorList().then((retrievedVendorList) => {
expect(retrievedVendorList.vendorListVersion).toEqual(VENDOR_LIST.vendorListVersion);
expect(retrievedVendorList).toEqual(VENDOR_LIST);
expect(pendingVendorlistPromise).toBeNull();
expect(cachedVendorList).toBeDefined();
});

loadVendorList().then((retrievedVendorList) => {
expect(fetchSpy.calls.count()).toBe(1);
done();
});
loadVendorList().then((retrievedVendorList) => {
expect(fetchSpy.calls.count()).toBe(1);
done();
});

expect(pendingVendorlistPromise).toBeDefined();
expect(cachedVendorList).toBeUndefined();

});

it('should use default vendor list if vendor list fetching fails', (done) => {
Expand Down

0 comments on commit 0bfd2d5

Please sign in to comment.