All files Link.js

83.33% Statements 5/6
100% Branches 2/2
100% Functions 1/1
83.33% Lines 5/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                            2x 9x 9x 9x 9x                
import { store, router } from './';
import { createElement } from 'react';
import { default as NextLink } from 'next/link';
 
/**
 * @description
 * Link component based on [next/link](https://github.com/zeit/next.js/#with-link)
 * It is possible to use this component exactly the same as Next.js <Link> component but it has some extra props.
 *
 * - **name** Route name
 * - **params** Route parameters (note: this is also the place to define your subdomain variables)
 * - **domain** Route domain (note: if the domain differs from the current active domain, push state won't be triggered)
 * - **query** Additional query parameters
 *
 * @function Link
 *
 * @param {Object} props Props that are used by next-avenues: (name:string, params:?Object, domain:?string, query:?object).
 * @returns {React.Element}
 *
 * @example
 * <Link name="article" domain="blog.next-avenues.com" params={{slug: 'lorem-ipsum'}} query={{ q: 'search' }} ><a>link</a><Link>
 */
const Link = ({ name, params, domain, protocol, query, ...newProps }) => {
    try {
        const route = store.find(name, domain || router.domain);
        const nextLinkProps = route.getNextLinkProps(params, { domain, protocol: protocol, query });
        return createElement(NextLink, { ...newProps, ...nextLinkProps });
    } catch (e) {
        // Fallback to Next.js Link component.
        return createElement(NextLink, { ...newProps });
    }
};
 
export default Link;