-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature to pick from multiple sample backends. (#2853)
* Initial work * Added magento backend validation. * Updated intercept to fetch backends. * Fetching sample backends while creating a pwa app. * Minor. * Added try catches. * Updated docs. * Minor. * Added node-fetch peer dep. * Minor pretty print stuff. * Added lodash and node-fetch deps. * Updated extension desc. * Updated tests. * Minor. * Updated remaining tests. * Added runEnvValidators.js tests. * Fixed linter issues. * Minor. * Added intercept tests. * Using debug instead of console.error. * Moving backend related code to run after configureWebpack. * Updated the skipped test. * Update ENV error reporting message. * Minor. * Minor. * Updated error snapshot test. * Minor. * Minor. * Added or condition for path variable in tests. * Minor. * Updated tests to use snapshots. * Mock everything, lets get this working. * Removed unecessary mocks. * Updated tests. * Using try catch to avoid prj creations. * Reporting different message if otherBackends is empty. * Updated tests. * Prettier fix. * Added console warning for production deployment. * Updated production launch checklist docs. Co-authored-by: Devagouda <40405790+dpatil-magento@users.noreply.github.com>
- Loading branch information
1 parent
c59de80
commit 936bd9e
Showing
28 changed files
with
586 additions
and
107 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
21 changes: 21 additions & 0 deletions
21
packages/extensions/venia-sample-backends/__tests__/__snapshots__/intercept.spec.js.snap
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,21 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`should call onFail if backend is inactive 1`] = ` | ||
"https://www.magento-backend-2.3.4.com/ is inactive. Please consider using one of these other backends: | ||
[{\\"name\\":\\"2.3.3-venia-cloud\\",\\"description\\":\\"Magento 2.3.3 with Venia sample data installed\\",\\"url\\":\\"https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/\\"}] | ||
" | ||
`; | ||
|
||
exports[`should call onFail with a different error message if environments is empty 1`] = ` | ||
"https://www.magento-backend-2.3.4.com/ is inactive. Please consider using one of these other backends: | ||
[{\\"name\\":\\"2.3.3-venia-cloud\\",\\"description\\":\\"Magento 2.3.3 with Venia sample data installed\\",\\"url\\":\\"https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/\\"}] | ||
" | ||
`; | ||
|
||
exports[`should log warning message in the console 1`] = ` | ||
" | ||
venia-sample-backends is a Dev only extension, please remove it from the project's package.json before going to production. | ||
" | ||
`; |
75 changes: 75 additions & 0 deletions
75
packages/extensions/venia-sample-backends/__tests__/intercept.spec.js
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,75 @@ | ||
jest.mock('node-fetch'); | ||
const fetch = require('node-fetch'); | ||
|
||
const { validateSampleBackend } = require('../intercept'); | ||
|
||
const env = { | ||
MAGENTO_BACKEND_URL: 'https://www.magento-backend-2.3.4.com/' | ||
}; | ||
const onFail = jest.fn().mockName('onFail'); | ||
const debug = jest.fn().mockName('debug'); | ||
|
||
const args = { env, onFail, debug }; | ||
|
||
beforeAll(() => { | ||
console.warn = jest.fn(); | ||
}); | ||
|
||
test('should not call onFail if backend is active', async () => { | ||
fetch.mockResolvedValueOnce({ ok: true }); | ||
|
||
await validateSampleBackend(args); | ||
|
||
expect(onFail).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should call onFail if backend is inactive', async () => { | ||
fetch.mockResolvedValueOnce({ ok: false }).mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValue({ | ||
sampleBackends: { | ||
environments: [ | ||
{ | ||
name: '2.3.3-venia-cloud', | ||
description: | ||
'Magento 2.3.3 with Venia sample data installed', | ||
url: | ||
'https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/' | ||
}, | ||
{ | ||
name: '2.3.4-venia-cloud', | ||
description: | ||
'Magento 2.3.4 with Venia sample data installed', | ||
url: 'https://www.magento-backend-2.3.4.com/' | ||
} | ||
] | ||
} | ||
}) | ||
}); | ||
|
||
await validateSampleBackend(args); | ||
|
||
expect(onFail).toHaveBeenCalled(); | ||
expect(onFail.mock.calls[0][0]).toMatchSnapshot(); | ||
}); | ||
|
||
test('should call onFail with a different error message if environments is empty', async () => { | ||
fetch.mockResolvedValueOnce({ ok: false }).mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValue({ | ||
sampleBackends: { | ||
environments: [] | ||
} | ||
}) | ||
}); | ||
|
||
await validateSampleBackend(args); | ||
|
||
expect(onFail).toHaveBeenCalled(); | ||
expect(onFail.mock.calls[0][0]).toMatchSnapshot(); | ||
}); | ||
|
||
test('should log warning message in the console', async () => { | ||
await validateSampleBackend(args); | ||
|
||
expect(console.warn).toHaveBeenCalled(); | ||
expect(console.warn.mock.calls[0][0]).toMatchSnapshot(); | ||
}); |
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,91 @@ | ||
const fetch = require('node-fetch'); | ||
|
||
const isBackendActive = async (env, debug) => { | ||
try { | ||
const magentoBackend = env.MAGENTO_BACKEND_URL; | ||
const res = await fetch(magentoBackend); | ||
|
||
return res.ok; | ||
} catch (err) { | ||
debug(err); | ||
|
||
return false; | ||
} | ||
}; | ||
|
||
const fetchBackends = async debug => { | ||
try { | ||
const res = await fetch( | ||
'https://fvp0esmt8f.execute-api.us-east-1.amazonaws.com/default/getSampleBackends' | ||
); | ||
const { sampleBackends } = await res.json(); | ||
|
||
return sampleBackends.environments; | ||
} catch (err) { | ||
debug(err); | ||
|
||
return []; | ||
} | ||
}; | ||
|
||
/** | ||
* Validation function to check if the backend being used is one of the sample backends provided | ||
* by PWA Studio. If yes, the function validates if the backend is active. If not, it reports an | ||
* error by calling the onFail function. In the error being reported, it sends the other sample | ||
* backends that the developers can use. | ||
* | ||
* @param {Object} config.env - The ENV provided to the app, usually avaialable through process.ENV | ||
* @param {Function} config.onFail - callback function to call on validation fail | ||
* @param {Function} config.debug - function to log debug messages in console in debug mode | ||
* | ||
* To watch the debug messages, run the command with DEBUG=*runEnvValidators* | ||
*/ | ||
const validateSampleBackend = async config => { | ||
console.warn( | ||
"\n venia-sample-backends is a Dev only extension, please remove it from the project's package.json before going to production. \n" | ||
); | ||
|
||
const { env, onFail, debug } = config; | ||
|
||
const backendIsActive = await isBackendActive(env, debug); | ||
|
||
if (!backendIsActive) { | ||
debug(`${env.MAGENTO_BACKEND_URL} is inactive`); | ||
|
||
debug('Fetching other backends'); | ||
|
||
const sampleBackends = await fetchBackends(debug); | ||
const otherBackends = sampleBackends.filter( | ||
({ url }) => url !== env.MAGENTO_BACKEND_URL | ||
); | ||
|
||
debug('PWA Studio supports the following backends', sampleBackends); | ||
|
||
debug('Reporting backend URL validation failure'); | ||
if (otherBackends.length) { | ||
onFail( | ||
`${ | ||
env.MAGENTO_BACKEND_URL | ||
} is inactive. Please consider using one of these other backends: \n\n ${JSON.stringify( | ||
otherBackends | ||
)} \n` | ||
); | ||
} else { | ||
onFail( | ||
`${ | ||
env.MAGENTO_BACKEND_URL | ||
} is inactive. Please consider using an active backend \n` | ||
); | ||
} | ||
} else { | ||
debug(`${env.MAGENTO_BACKEND_URL} is active`); | ||
} | ||
}; | ||
|
||
module.exports = targets => { | ||
targets | ||
.of('@magento/pwa-buildpack') | ||
.validateEnv.tapPromise(validateSampleBackend); | ||
}; | ||
|
||
module.exports.validateSampleBackend = validateSampleBackend; |
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,24 @@ | ||
{ | ||
"name": "@magento/venia-sample-backends", | ||
"version": "0.0.1", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Provides demo backends and backend validation utils for PWA Studio.", | ||
"main": "./intercept.js", | ||
"scripts": { | ||
"clean": " ", | ||
"test": "jest" | ||
}, | ||
"repository": "github:magento/pwa-studio", | ||
"license": "(OSL-3.0 OR AFL-3.0)", | ||
"peerDependencies": { | ||
"@magento/pwa-buildpack": "~7.0.0", | ||
"node-fetch": "~2.3.0" | ||
}, | ||
"pwa-studio": { | ||
"targets": { | ||
"intercept": "./intercept" | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -20,4 +20,4 @@ | |
"intercept": "./i18n-intercept" | ||
} | ||
} | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
packages/pwa-buildpack/lib/Utilities/__tests__/__snapshots__/runEnvValidators.spec.js.snap
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,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`should throw error if there are validation errors reported by interceptors 1`] = ` | ||
"Environment has 2 validation errors: | ||
(1) Danger, | ||
(2) Another error" | ||
`; |
Oops, something went wrong.