-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/support-bigint
- Loading branch information
Showing
9 changed files
with
191 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
version: 2 | ||
version: 2.1 | ||
|
||
orbs: | ||
node: circleci/[email protected] | ||
|
||
jobs: | ||
test: | ||
test-with-node: | ||
parameters: | ||
node-version: | ||
type: string | ||
docker: | ||
- image: circleci/node:10 | ||
- image: cimg/base:stable | ||
steps: | ||
- checkout | ||
- node/install: | ||
node-version: << parameters.node-version >> | ||
install-yarn: true | ||
- run: yarn install | ||
- run: yarn build | ||
- run: yarn lint | ||
- run: yarn test | ||
|
||
workflows: | ||
version: 2 | ||
|
||
build_and_test: | ||
jobs: | ||
- test | ||
- test-with-node: | ||
matrix: | ||
parameters: | ||
node-version: ["12", "14", "16"] |
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,48 @@ | ||
const eventProperties: Array<keyof Event> = [ | ||
'bubbles', | ||
'cancelBubble', | ||
'cancelable', | ||
'composed', | ||
'currentTarget', | ||
'defaultPrevented', | ||
'eventPhase', | ||
'isTrusted', | ||
'returnValue', | ||
'srcElement', | ||
'target', | ||
'timeStamp', | ||
'type', | ||
]; | ||
|
||
const customEventSpecificProperties: Array<Exclude<keyof CustomEvent, keyof Event>> = ['detail']; | ||
|
||
/** | ||
* Dom Event (and all its subclasses) is built in a way its internal properties | ||
* are accessible when querying them directly but "hidden" when iterating its | ||
* keys. | ||
* | ||
* With a code example it means: `Object.keys(new Event('click')) = ["isTrusted"]` | ||
* | ||
* So to be able to stringify/parse more than just `isTrusted` info we need to | ||
* create a new object and set the properties by hand. As there is no way to | ||
* iterate the properties we rely on a list of hardcoded properties. | ||
* | ||
* @param event The event we want to extract properties | ||
*/ | ||
export function extractEventHiddenProperties(event: Event): unknown { | ||
const rebuildEvent = eventProperties | ||
.filter((value) => event[value] !== undefined) | ||
.reduce((acc, value) => { | ||
return { ...acc, [value]: event[value] }; | ||
}, {} as any); | ||
|
||
if (event instanceof CustomEvent) { | ||
customEventSpecificProperties | ||
.filter((value) => event[value] !== undefined) | ||
.forEach((value) => { | ||
rebuildEvent[value] = event[value]; | ||
}); | ||
} | ||
|
||
return rebuildEvent; | ||
} |
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,81 @@ | ||
import * as src from '../../src'; | ||
import * as dist from '../../dist'; | ||
|
||
const tests = ({ stringify, parse }) => { | ||
test('HTML Event', () => { | ||
const event = new MouseEvent('click', { bubbles: true, composed: true, cancelable: true }); | ||
|
||
const stringified = stringify(event); | ||
|
||
const parsed = parse(stringified); | ||
|
||
expect(parsed).toMatchObject({ | ||
isTrusted: expect.any(Boolean), | ||
type: 'click', | ||
bubbles: true, | ||
composed: true, | ||
cancelable: true, | ||
returnValue: expect.any(Boolean), | ||
timeStamp: expect.any(Number), | ||
}); | ||
}); | ||
|
||
test('HTML Custom Event', () => { | ||
const event = new CustomEvent('custom:click', { | ||
bubbles: true, | ||
composed: true, | ||
cancelable: true, | ||
detail: { aKey: 'a Value' }, | ||
}); | ||
|
||
const stringified = stringify(event, { allowClass: true }); | ||
|
||
const parsed = parse(stringified); | ||
|
||
expect(parsed).toMatchObject({ | ||
isTrusted: expect.any(Boolean), | ||
type: 'custom:click', | ||
bubbles: true, | ||
composed: true, | ||
cancelable: true, | ||
returnValue: expect.any(Boolean), | ||
timeStamp: expect.any(Number), | ||
detail: { aKey: 'a Value' }, | ||
}); | ||
}); | ||
|
||
test('Nested HTML Custom Event', () => { | ||
const event = new CustomEvent('custom:click', { | ||
bubbles: true, | ||
composed: true, | ||
cancelable: true, | ||
detail: { aKey: 'a Value' }, | ||
}); | ||
|
||
const stringified = stringify({ key: 'value', args: event }, { allowClass: true }); | ||
|
||
const parsed = parse(stringified); | ||
|
||
expect(parsed).toMatchObject({ | ||
key: 'value', | ||
args: { | ||
isTrusted: expect.any(Boolean), | ||
type: 'custom:click', | ||
bubbles: true, | ||
composed: true, | ||
cancelable: true, | ||
returnValue: expect.any(Boolean), | ||
timeStamp: expect.any(Number), | ||
detail: { aKey: 'a Value' }, | ||
}, | ||
}); | ||
}); | ||
}; | ||
|
||
describe('Source', () => { | ||
tests(src); | ||
}); | ||
|
||
describe('Dist', () => { | ||
tests(dist); | ||
}); |
File renamed without changes.
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,16 @@ | ||
import * as src from '../../src'; | ||
import * as dist from '../../dist'; | ||
|
||
const tests = ({ stringify }) => { | ||
test('stringify the global object', () => { | ||
expect(() => stringify(global, { maxDepth: 10000 })).not.toThrow(); | ||
}); | ||
}; | ||
|
||
describe('Source', () => { | ||
tests(src); | ||
}); | ||
|
||
describe('Dist', () => { | ||
tests(dist); | ||
}); |