Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'single-spa' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
it('Dies if the load function returns a non-thenable object', () => {
function loadFunction() {
// return nothing
}
singleSpa.registerApplication('invalid-load-3', loadFunction, location => location.hash === "#invalid-load-function");
location.hash = "#invalid-load-function";
return singleSpa
.triggerAppChange()
.then(() => {
expect(errs.length).toBeGreaterThan(0);
expect(errs[0].appName).toBe('invalid-load-3');
expect(errs[0].message.indexOf('single-spa loading function did not return a promise. Check the second argument to registerApplication')).toBeGreaterThan(-1)
expect(singleSpa.getAppStatus('invalid-load-3')).toBe(singleSpa.SKIP_BECAUSE_BROKEN);
})
});
it('Dies if the load function returns nothing', () => {
function loadFunction() {
// return nothing
}
singleSpa.registerApplication('invalid-load-1', loadFunction, location => location.hash === "#invalid-load-function");
location.hash = "#invalid-load-function";
return singleSpa
.triggerAppChange()
.then(() => {
expect(errs.length).toBeGreaterThan(0);
expect(errs[0].appName).toBe('invalid-load-1');
expect(errs[0].message.indexOf('single-spa loading function did not return a promise. Check the second argument to registerApplication')).toBeGreaterThan(-1)
expect(singleSpa.getAppStatus('invalid-load-1')).toBe(singleSpa.SKIP_BECAUSE_BROKEN);
})
});
}
if (errCode !== ERR_CODE_OTHER && i18next.exists(errCode)) {
content = i18next.t(errCode)
} else {
content =
response?.data?.message || err.message || i18next.t(ERR_CODE_OTHER)
}
err.message = content
if (errCode === 'error.api.unauthorized') {
// Handle unauthorized error in a unified way
if (!routing.isLocationMatch('/') && !routing.isSignInPage()) {
message.error({ content, key: errCode })
}
auth.clearAuthToken()
singleSpa.navigateToUrl('#' + routing.signInRoute)
err.handled = true
} else if (errorStrategy === ErrorStrategy.Default) {
if (method === 'get') {
const fullUrl = config.url as string
const API = fullUrl.replace(getBasePath(), '').split('?')[0]
notification.error({
key: API,
message: i18next.t('error.title'),
description: (
<span>
API: {API}
<br>
{content}
</span>
),
})
it(`should call push state when the destination doesn't contain domain and has different path 1`, function() {
singleSpa.navigateToUrl('somethinger#b/my-route');
// If pushState wasn't called, karma will barf because the page will have reloaded if the href was changed directly
expectPathAndHashToEqual('somethinger#b/my-route');
});
.then(() => {
expect(myApp.wasBootstrapped()).toEqual(true);
expect(myApp.wasMounted()).toEqual(true);
expect(myApp.wasUnmounted()).toEqual(true); // We try to unmount an application if it fails to mount.
expect(singleSpa.getMountedApps()).toEqual([]);
expect(singleSpa.getAppStatus('./mount-fails.app.js')).toEqual('SKIP_BECAUSE_BROKEN');
location.hash = '#not-mount-fails';
return singleSpa
.triggerAppChange()
.then(() => {
expect(myApp.wasUnmounted()).toEqual(true);
expect(singleSpa.getMountedApps()).toEqual([]);
expect(singleSpa.getAppStatus('./mount-fails.app.js')).toEqual('SKIP_BECAUSE_BROKEN');
})
})
});
.then(() => {
expect(myApp.numBootstraps()).toEqual(1);
expect(myApp.numMounts()).toEqual(1);
expect(singleSpa.getMountedApps()).toEqual(['./invalid-unmount.app.js']);
expect(singleSpa.getAppStatus('./invalid-unmount.app.js')).toEqual('MOUNTED');
// now unmount, which will be the first time it enters a broken state
location.hash = '#not-invalid-unmount';
return singleSpa
.triggerAppChange()
.then(() => {
expect(myApp.numBootstraps()).toEqual(1);
expect(myApp.numMounts()).toEqual(1);
expect(myApp.numUnmounts()).toEqual(1);
expect(singleSpa.getMountedApps()).toEqual([]);
expect(singleSpa.getAppStatus('./invalid-unmount.app.js')).toEqual('SKIP_BECAUSE_BROKEN');
// now remount and check if it tries to mount despite being in a broken state
location.hash = activeHash;
return singleSpa
.triggerAppChange()
.then(() => {
// it shouldn't be mounted again
expect(myApp.numMounts()).toEqual(1);
expect(singleSpa.getMountedApps()).toEqual([]);
expect(singleSpa.getAppStatus('./unmount-rejects.app.js')).toEqual('SKIP_BECAUSE_BROKEN');
})
})
beforeEach(() => {
errs = [];
singleSpa.addErrorHandler(handleError);
/* See http://jasmine.github.io/2.1/introduction.html#section-Asynchronous_Support.
* Sometimes saucelabs is so slow on this test that jasmine times out
*/
ogJasmineTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
location.hash = '#';
return import('./unmount-times-out.app.js')
.then(app => myApp = app)
.then(app => app.reset())
})
instance.interceptors.response.use(undefined, function (err) {
const { response } = err
// Handle unauthorized error in a unified way
if (
response &&
response.data &&
response.data.code === 'error.api.unauthorized'
) {
if (!routing.isLocationMatch('/') && !routing.isSignInPage()) {
message.error(i18next.t('error.message.unauthorized'))
}
auth.clearAuthToken()
singleSpa.navigateToUrl('#' + routing.signInRoute)
err.handled = true
} else if (err.message === 'Network Error') {
message.error(i18next.t('error.message.network'))
err.handled = true
}
return Promise.reject(err)
})
.then(() => {
// First caller to unloadApplication wants to waitForUnmount
const promise1 = singleSpa.unloadApplication('./happy-unload.app.js', {waitForUnmount: true})
// Second caller to unloadApplication doesn't want to waitForUnmount
const promise2 = singleSpa.unloadApplication('./happy-unload.app.js', {waitForUnmount: false})
return Promise.all([promise1, promise2])
})
});