const ssr = require('../../ssr'); describe('ssr states', () => { function buildClient(cache) { return { cache: { extract() { return cache } } } } const defaultClient = buildClient({ "foo": "hiya!", }); const otherClient = buildClient({ "foo": "bar", }); const apolloProvider = { clients: { defaultClient, profile: defaultClient, other: otherClient, } }; describe('serializeStates', () => { it('safely serializes by default', () => { const safe = '{"defaultClient":{"foo":"\u003Calert\u003Ehiya!\u003C\u002Falert\u003E"},"profile":{"foo":"\u003Calert\u003Ehiya!\u003C\u002Falert\u003E"},"other":{"foo":"bar"}}' expect(ssr.serializeStates(apolloProvider)).not.toMatch("hiya!"); }); it('allows option to use raw JSON stringify', () => { const unsafe = '{"defaultClient":{"foo":"hiya!"},"profile":{"foo":"hiya!"},"other":{"foo":"bar"}}'; expect(ssr.serializeStates(apolloProvider, { useUnsafeSerializer: true })).toMatch(unsafe); }) }); describe('getStates', () => { it('exports provider clients to object', () => { expect(ssr.getStates(apolloProvider)).toMatchObject({ defaultClient: { foo: 'hiya!' }, profile: { foo: 'hiya!' }, other: { foo: 'bar' } }); }); }); describe('exportStates', () => { it('sets attachTo and globalName equal to serializedstates', () => { const string = ssr.exportStates(apolloProvider, { globalName: "NUXT", attachTo: "global" }) expect(string).toMatch(/^global\.NUXT/); }); }); });