File size: 3,838 Bytes
1e92f2d |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import { shouldServerSideRender, setShouldServerSideRender } from '..';
/**
* A mutable hashmap that is used to create the current context (i.e. the values set here can be used as return
* values for some specific methods that are being called). Each test can override these values by calling the remock
* function.
* @see remock function for the default values.
* @type {{ojbect}}
*/
let mockReturnValues = {};
/**
* Updates the values returned by the mocks. Combines the given parameters with the default ones, such that the the
* tests can focus on what they really want to change and express that in the code.
* @see mockReturnValues
* @param {{object}} newReturnValues A key value set of properties that are combined with the defaults.
*/
function remock( newReturnValues ) {
mockReturnValues = Object.assign(
{
configServerSideRender: true,
},
newReturnValues
);
}
jest.mock( '@automattic/calypso-config', () => {
const fn = () => {};
fn.isEnabled = ( feature_key ) =>
feature_key === 'server-side-rendering' ? mockReturnValues.configServerSideRender : false;
return fn;
} );
const ssrCompatibleContext = {
section: {
isomorphic: true,
},
layout: 'hello',
user: null,
lang: 'en',
};
const ssrEnabledContext = {
...ssrCompatibleContext,
serverSideRender: true,
};
const ssrDisabledContext = {
...ssrCompatibleContext,
serverSideRender: false,
};
describe( 'shouldServerSideRender', () => {
beforeEach( () => remock() );
test( 'feature-flag server-side-render should enable SSR (default behavior)', () => {
expect( shouldServerSideRender( ssrEnabledContext ) ).toBe( true );
} );
test( 'feature-flag server-side-render should disable SSR', () => {
remock( { configServerSideRender: false } );
expect( shouldServerSideRender( ssrEnabledContext ) ).toBe( false );
} );
test( 'context.serverSideRender should alter the result', () => {
expect( shouldServerSideRender( ssrCompatibleContext ) ).toBe( false ); // due to undefined
expect( shouldServerSideRender( ssrEnabledContext ) ).toBe( true );
expect( shouldServerSideRender( ssrDisabledContext ) ).toBe( false );
} );
test( 'context.layout should alter the result', () => {
expect( shouldServerSideRender( ssrEnabledContext ) ).toBe( true );
const SsrEnabledContextWithoutLayout = {
...ssrEnabledContext,
layout: undefined,
};
expect( shouldServerSideRender( SsrEnabledContextWithoutLayout ) ).toBe( false );
} );
test( 'context.user should alter the result', () => {
expect( shouldServerSideRender( ssrEnabledContext ) ).toBe( true );
const ssrEnabledContextWithUser = {
...ssrEnabledContext,
user: {
name: 'hello-world',
},
};
expect( shouldServerSideRender( ssrEnabledContextWithUser ) ).toBe( false );
} );
test( 'non-isomorphic section should disable SSR', () => {
const ssrNonIsomorphicSectionContext = {
...ssrEnabledContext,
section: { isomorphic: false },
};
expect( shouldServerSideRender( ssrNonIsomorphicSectionContext ) ).toBe( false );
} );
} );
describe( 'setShouldServerSideRender', () => {
test( 'when query is empty, then sets context.serverSideRender to TRUE - and calls next()', () => {
const next = jest.fn();
const contextWithoutQueryKeys = {
query: {},
};
setShouldServerSideRender( contextWithoutQueryKeys, next );
expect( contextWithoutQueryKeys.serverSideRender ).toBe( true );
expect( next ).toHaveBeenCalledTimes( 1 );
} );
test( 'when query has values, then sets context.serverSideRender to FALSE - and calls next()', () => {
const next = jest.fn();
const contextWithQueryKeys = {
query: {
hello: 'world',
},
};
setShouldServerSideRender( contextWithQueryKeys, next );
expect( contextWithQueryKeys.serverSideRender ).toBe( false );
expect( next ).toHaveBeenCalledTimes( 1 );
} );
} );
|