-
Notifications
You must be signed in to change notification settings - Fork 46
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
chore: added tests for fragment hooks #298
Merged
Merged
Changes from 2 commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
const chai = require('chai'); | ||
const {getFragmentAttributes} = require('../../tests/helpers'); | ||
const {insertStart, insertEnd} = require('./fragment-hooks'); | ||
const {PassThrough} = require('stream'); | ||
|
||
describe('fragment-hooks', () => { | ||
describe('insertEnd', () => { | ||
it('should not write to stream', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes(); | ||
const headers = {}; | ||
|
||
insertEnd(mockStream, fragmentAttrs, headers, 0); | ||
|
||
chai.expect(mockStream.readableLength).to.be.equal(0); | ||
}); | ||
}); | ||
|
||
describe('insertStart', () => { | ||
it('should write a script tag with wrapper overrides', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({ | ||
wrapperPropsOverride: { | ||
param1: 'value1', | ||
param2: 'value2', | ||
} | ||
}); | ||
const headers = {}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
|
||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal('<script type="spa-config-override">{"wrapperPropsOverride":{"param1":"value1","param2":"value2"}}</script>'); | ||
}); | ||
|
||
|
||
it('should write async fragment code warning comments for ref stylesheet ', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({async: true}); | ||
const headers = { | ||
link: '<https://domain.com/app.css>; rel="stylesheet"', | ||
}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
|
||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal( | ||
'<!-- Async fragments are not fully implemented yet: https://domain.com/app.css -->' + | ||
'<script type="spa-config-override">{"cssBundle":"https://domain.com/app.css"}</script>' | ||
); | ||
|
||
}); | ||
|
||
it('should inject style link override function and script config override tag', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({id: 'autogenerated_identifier'}); | ||
const headers = { | ||
link: '<https://domain.com/app.css>; rel="stylesheet"', | ||
}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
|
||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal( | ||
'<script>(function(url, id){const link = document.head.querySelector(\'link[data-fragment-id="\' + id + \'"]\');if (link && link.href !== url) {link.href = url;}})("https://domain.com/app.css", "autogenerated_identifier");</script>' + | ||
'<script type="spa-config-override">{"cssBundle":"https://domain.com/app.css"}</script>' | ||
); | ||
}); | ||
|
||
it('shouldn\'t inject style link override function if attribute.id hasn\'t been provided', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({id: ''}); | ||
const headers = { | ||
link: '<https://domain.com/app.css>; rel="stylesheet"', | ||
}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
|
||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal( | ||
'<script type="spa-config-override">{"cssBundle":"https://domain.com/app.css"}</script>' | ||
); | ||
}); | ||
|
||
it('should inject overrides with spaBundle', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({id: 'autogenerated_identifier'}); | ||
const headers = { | ||
link: '<https://domain.com/single_spa.js>; rel="fragment-script"; as="script"; crossorigin="anonymous"', | ||
}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal('<script type="spa-config-override">{"spaBundle":"https://domain.com/single_spa.js","appName":"autogenerated_identifier"}</script>'); | ||
}); | ||
|
||
it('should correct map appName for overrides', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({ | ||
id: 'autogenerated_identifier', | ||
wrapperConf: { | ||
appId: 'wrapper__at__body', | ||
name: '@portal/wrapper', | ||
src: 'https://wrapper.com/fragment', | ||
timeout: 2000 | ||
}, | ||
}); | ||
|
||
const headers = { | ||
link: '<https://domain.com/single_spa.js>; rel="fragment-script"; as="script"; crossorigin="anonymous"', | ||
}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal('<script type="spa-config-override">{"spaBundle":"https://domain.com/single_spa.js","appName":"@portal/wrapper"}</script>'); | ||
}); | ||
|
||
it('should forward fragment dependency through overrides', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({id: 'autogenerated_identifier'}); | ||
const headers = { | ||
link: '<https://domain.com/single_spa.js>; rel="fragment-dependency"; name="dep_name"', | ||
}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
|
||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal('<script type="spa-config-override">{"dependencies":{"dep_name":"https://domain.com/single_spa.js"}}</script>'); | ||
}); | ||
|
||
it('should forward a few fragment dependencies through overrides', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({id: 'autogenerated_identifier'}); | ||
const headers = { | ||
link: '<https://domain.com/single_spa.js>; rel="fragment-dependency"; name="dep_name", <https://domain2.com/single_spa2.js>; rel="fragment-dependency"; name="dep_name2"', | ||
}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
|
||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal('<script type="spa-config-override">{"dependencies":{"dep_name":"https://domain.com/single_spa.js","dep_name2":"https://domain2.com/single_spa2.js"}}</script>'); | ||
}); | ||
|
||
it('shouldn\'t forward fragment dependency uri if its name hasn\'t been provided', () => { | ||
const mockStream = new PassThrough(); | ||
const fragmentAttrs = getFragmentAttributes({id: 'autogenerated_identifier'}); | ||
const headers = { | ||
link: '<https://domain.com/single_spa.js>; rel="fragment-script"; as="script"; crossorigin="anonymous", <https://domain.com/single_spa.js>; rel="fragment-dependency";', | ||
}; | ||
|
||
insertStart(mockStream, fragmentAttrs, headers, 0); | ||
|
||
const streamData = mockStream.read(); | ||
|
||
chai.expect(streamData).to.be.instanceOf(Buffer); | ||
chai.expect(streamData.toString()).to.be.equal('<script type="spa-config-override">{"spaBundle":"https://domain.com/single_spa.js","appName":"autogenerated_identifier"}</script>'); | ||
}) | ||
}); | ||
}); |
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
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.
looks like "index" isn't used inside "insertStart", probably we can remove it from module and from tests too
offtop: I think would be better to create a variable before providing raw parameter to a function, since it's unreal to understand what is the fourth parameter, e.g.