forked from yahoo/serialize-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replaced crypto shim with separate entrypoints for node and bro…
…wser environments
- Loading branch information
Egor Manzhula
committed
Nov 13, 2024
1 parent
8dd6c19
commit 566aedf
Showing
9 changed files
with
145 additions
and
504 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,10 @@ | ||
import { Serializer } from './Serializer'; | ||
import { generateUUID } from './generateUUID'; | ||
|
||
const uuid = generateUUID(window.crypto); | ||
const serializer = new Serializer(uuid); | ||
|
||
const serialize = serializer.serialize.bind(serializer); | ||
|
||
export { serialize }; | ||
export default serialize; |
This file was deleted.
Oops, something went wrong.
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,64 @@ | ||
import sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
import * as generateUUIDModule from '../../src/generateUUID'; | ||
import * as placeholderSerializerModule from '../../src/Serializer/PlaceholderSerializer'; | ||
|
||
describe('Browser entrypoint', () => { | ||
let serializeStub: sinon.SinonStub; | ||
let generateUUIDStub: sinon.SinonStub; | ||
let placeholderSerializerSpy: sinon.SinonSpy; | ||
let serializeFn; | ||
let cryptoMock = {}; | ||
|
||
before(() => { | ||
// Mock the `window` and `window.crypto` if they are not defined | ||
if (typeof global.window === 'undefined') { | ||
(global as any).window = {}; | ||
} | ||
|
||
Object.defineProperty(global.window, 'crypto', { | ||
value: cryptoMock, | ||
configurable: true, | ||
writable: true, | ||
}); | ||
|
||
generateUUIDStub = sinon.stub(generateUUIDModule, 'generateUUID'); | ||
generateUUIDStub.returns('00000000000000000000000000000'); | ||
|
||
placeholderSerializerSpy = sinon.spy(placeholderSerializerModule, 'PlaceholderSerializer'); | ||
|
||
serializeStub = sinon.stub(placeholderSerializerModule.PlaceholderSerializer.prototype, 'serialize').returns('serialized-data'); | ||
|
||
const { serialize } = require('../../src/browser'); | ||
serializeFn = serialize; | ||
}); | ||
|
||
after(() => { | ||
Object.defineProperty(global.window, 'crypto', { | ||
value: undefined, | ||
configurable: true, | ||
writable: true, | ||
}); | ||
|
||
sinon.restore(); | ||
}); | ||
|
||
it('should invoke genereUUID function with window.crypto', () => { | ||
expect(generateUUIDStub.calledOnce).to.be.true; | ||
expect(generateUUIDStub.calledWith(cryptoMock)).to.be.true; | ||
}); | ||
|
||
it('should create PlaceholderSerializer instance with UUID returned by generateUUID', () => { | ||
expect(placeholderSerializerSpy.calledOnce).to.be.true; | ||
expect(placeholderSerializerSpy.calledWith('00000000000000000000000000000')).to.be.true; | ||
}); | ||
|
||
it('should invoke PlaceholderSerializer.serialize and return it\'s result', () => { | ||
const toSerialize = { test: 1} | ||
const serialized = serializeFn(toSerialize); | ||
|
||
expect(serializeStub.calledOnce).to.be.true; | ||
expect(serializeStub.calledWith(toSerialize)).to.be.true; | ||
expect(serialized).to.be.equal('serialized-data'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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