-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Move ready queue processing after identity request #933
Open
alexs-mparticle
wants to merge
8
commits into
development
Choose a base branch
from
fix/SQDSDKS-6835-event-logging-wo-identities
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6478269
fix: Move ready queue processing after identity request
alexs-mparticle 637c107
Fix failing tests
alexs-mparticle 68c73e1
Fix failing tests
alexs-mparticle 94627d5
Clean up lint
alexs-mparticle 198b448
Add tests for sequencing
alexs-mparticle 60ec32c
Add test coverage
alexs-mparticle 7354cdf
Address PR Comments
alexs-mparticle f411dda
Move test sequence order
alexs-mparticle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'); | ||
}); | ||
rmi22186 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this
args
function necessary? I haven't pulled this code down, but as I read through theprocessReadyQueue
function, the only times I seeargs
is where it references the item in the array, as opposed to it being a key onwindow.mParticle
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to discussing this further. Adding
args
here was the only way I could get it to hit that specific codepath in my tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's hop on a zoom about this to walk through
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm. when i comment out line 32, it passes for me. see screenshot below: