-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move ready queue processing after identity request (#933)
- Loading branch information
1 parent
261f33b
commit 678ccb7
Showing
14 changed files
with
377 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { isEmpty, isFunction } from './utils'; | ||
|
||
export const processReadyQueue = (readyQueue): Function[] => { | ||
if (!isEmpty(readyQueue)) { | ||
readyQueue.forEach(readyQueueItem => { | ||
if (isFunction(readyQueueItem)) { | ||
readyQueueItem(); | ||
} else if (Array.isArray(readyQueueItem)) { | ||
processPreloadedItem(readyQueueItem); | ||
} | ||
}); | ||
} | ||
return []; | ||
}; | ||
|
||
const processPreloadedItem = (readyQueueItem): void => { | ||
const args = readyQueueItem; | ||
const method = args.splice(0, 1)[0]; | ||
|
||
// if the first argument is a method on the base mParticle object, run it | ||
if (typeof window !== 'undefined' && window.mParticle && window.mParticle[args[0]]) { | ||
window.mParticle[method].apply(this, args); | ||
// otherwise, the method is on either eCommerce or Identity objects, ie. "eCommerce.setCurrencyCode", "Identity.login" | ||
} else { | ||
const methodArray = method.split('.'); | ||
try { | ||
let computedMPFunction = window.mParticle; | ||
for (const currentMethod of methodArray) { | ||
computedMPFunction = computedMPFunction[currentMethod]; | ||
} | ||
((computedMPFunction as unknown) as Function).apply(this, args); | ||
} catch (e) { | ||
throw new Error('Unable to compute proper mParticle function ' + e); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { processReadyQueue } from '../../src/pre-init-utils'; | ||
|
||
describe('pre-init-utils', () => { | ||
describe('#processReadyQueue', () => { | ||
it('should return an empty array if readyQueue is empty', () => { | ||
const result = processReadyQueue([]); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should process functions passed as arguments', () => { | ||
const functionSpy = jest.fn(); | ||
const readyQueue: Function[] = [functionSpy, functionSpy, functionSpy]; | ||
const result = processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalledTimes(3); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should process functions passed as arrays', () => { | ||
const functionSpy = jest.fn(); | ||
(window.mParticle as any) = { | ||
fakeFunction: functionSpy, | ||
}; | ||
const readyQueue = [['fakeFunction']]; | ||
processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should process functions passed as arrays with arguments', () => { | ||
const functionSpy = jest.fn(); | ||
(window.mParticle as any) = { | ||
fakeFunction: functionSpy, | ||
args: () => {}, | ||
}; | ||
const readyQueue = [['fakeFunction', 'args']]; | ||
processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalledWith('args'); | ||
}); | ||
|
||
it('should process arrays passed as arguments with multiple methods', () => { | ||
const functionSpy = jest.fn(); | ||
(window.mParticle as any) = { | ||
fakeFunction: { | ||
anotherFakeFunction: functionSpy, | ||
}, | ||
}; | ||
const readyQueue = [['fakeFunction.anotherFakeFunction', 'foo']]; | ||
processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalledWith('foo'); | ||
}); | ||
|
||
it('should process arrays passed as arguments with multiple methods and arguments', () => { | ||
const functionSpy = jest.fn(); | ||
const functionSpy2 = jest.fn(); | ||
(window.mParticle as any) = { | ||
fakeFunction: functionSpy, | ||
anotherFakeFunction: functionSpy2, | ||
}; | ||
const readyQueue = [['fakeFunction', 'foo'], ['anotherFakeFunction', 'bar']]; | ||
processReadyQueue(readyQueue); | ||
expect(functionSpy).toHaveBeenCalledWith('foo'); | ||
expect(functionSpy2).toHaveBeenCalledWith('bar'); | ||
}); | ||
|
||
it('should throw an error if it cannot compute the proper mParticle function', () => { | ||
const readyQueue = [['Identity.login']]; | ||
expect(() => processReadyQueue(readyQueue)).toThrowError("Unable to compute proper mParticle function TypeError: Cannot read properties of undefined (reading 'login')"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.