|
| 1 | +import { generatePageMetaData } from './generatePageMetaData'; |
| 2 | + |
| 3 | +const args: any = { |
| 4 | + actions: {}, |
| 5 | + page: {}, |
| 6 | + reporter: {}, |
| 7 | +}; |
| 8 | + |
| 9 | +jest.mock('../../../content/i18n/de-CH.json', () => ({}), { virtual: true }); |
| 10 | + |
| 11 | +describe('generatePageMetaData', () => { |
| 12 | + beforeEach(() => { |
| 13 | + args.actions = { |
| 14 | + createPage: jest.fn(), |
| 15 | + deletePage: jest.fn(), |
| 16 | + }; |
| 17 | + args.page = {}; |
| 18 | + args.reporter = { |
| 19 | + warn: jest.fn(), |
| 20 | + }; |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns without page context', async () => { |
| 24 | + await generatePageMetaData(args); |
| 25 | + expect(args.actions.deletePage).not.toBeCalled(); |
| 26 | + expect(args.actions.createPage).not.toBeCalled(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns when is not a stateful created page', async () => { |
| 30 | + args.page = { |
| 31 | + context: {}, |
| 32 | + isCreatedByStatefulCreatePages: false, |
| 33 | + }; |
| 34 | + await generatePageMetaData(args); |
| 35 | + expect(args.actions.deletePage).not.toBeCalled(); |
| 36 | + expect(args.actions.createPage).not.toBeCalled(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns without locale pages id', async () => { |
| 40 | + args.page = { |
| 41 | + context: {}, |
| 42 | + isCreatedByStatefulCreatePages: true, |
| 43 | + }; |
| 44 | + await generatePageMetaData(args); |
| 45 | + expect(args.actions.deletePage).not.toBeCalled(); |
| 46 | + expect(args.actions.createPage).not.toBeCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns when locale pages id is blacklisted', async () => { |
| 50 | + args.page = { |
| 51 | + context: { |
| 52 | + localePagesId: 'dev-404-page', |
| 53 | + }, |
| 54 | + isCreatedByStatefulCreatePages: true, |
| 55 | + }; |
| 56 | + await generatePageMetaData(args); |
| 57 | + expect(args.actions.deletePage).not.toBeCalled(); |
| 58 | + expect(args.actions.createPage).not.toBeCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns with warning when there are no messages for locale', async () => { |
| 62 | + args.page = { |
| 63 | + context: { |
| 64 | + localePagesId: 'some.page', |
| 65 | + }, |
| 66 | + isCreatedByStatefulCreatePages: true, |
| 67 | + }; |
| 68 | + await generatePageMetaData(args); |
| 69 | + expect(args.reporter.warn).toBeCalledWith(`Page context locale is null or couldn't find locale undefined.`); |
| 70 | + expect(args.actions.deletePage).not.toBeCalled(); |
| 71 | + expect(args.actions.createPage).not.toBeCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns with warning when the key site.meta.title is not in the messages', async () => { |
| 75 | + args.page = { |
| 76 | + context: { |
| 77 | + localePagesId: 'index', |
| 78 | + locale: 'de-CH', |
| 79 | + }, |
| 80 | + isCreatedByStatefulCreatePages: true, |
| 81 | + }; |
| 82 | + await generatePageMetaData(args); |
| 83 | + expect(args.reporter.warn).toBeCalledWith(`Missing key site.meta.title in messages.`); |
| 84 | + expect(args.actions.deletePage).not.toBeCalled(); |
| 85 | + expect(args.actions.createPage).not.toBeCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('set the meta data with warning when the page title key is not in the messages', async () => { |
| 89 | + args.page = { |
| 90 | + context: { |
| 91 | + localePagesId: 'some.fictional.page', |
| 92 | + locale: 'en-US', |
| 93 | + }, |
| 94 | + isCreatedByStatefulCreatePages: true, |
| 95 | + }; |
| 96 | + await generatePageMetaData(args); |
| 97 | + expect(args.reporter.warn).toBeCalledWith(`Missing key page.some.fictional.page.meta.title in messages.`); |
| 98 | + expect(args.reporter.warn).toBeCalledWith(`Missing key page.some.fictional.page.meta.description in messages.`); |
| 99 | + expect(args.actions.createPage).toMatchInlineSnapshot(` |
| 100 | + [MockFunction] { |
| 101 | + "calls": [ |
| 102 | + [ |
| 103 | + { |
| 104 | + "context": { |
| 105 | + "locale": "en-US", |
| 106 | + "localePagesId": "some.fictional.page", |
| 107 | + "metaData": { |
| 108 | + "description": undefined, |
| 109 | + "title": "openscript Ltd.", |
| 110 | + }, |
| 111 | + }, |
| 112 | + "isCreatedByStatefulCreatePages": true, |
| 113 | + }, |
| 114 | + ], |
| 115 | + ], |
| 116 | + "results": [ |
| 117 | + { |
| 118 | + "type": "return", |
| 119 | + "value": undefined, |
| 120 | + }, |
| 121 | + ], |
| 122 | + } |
| 123 | + `); |
| 124 | + }); |
| 125 | + |
| 126 | + it('sets the meta data', async () => { |
| 127 | + args.page = { |
| 128 | + context: { |
| 129 | + localePagesId: 'index', |
| 130 | + locale: 'en-US', |
| 131 | + }, |
| 132 | + isCreatedByStatefulCreatePages: true, |
| 133 | + }; |
| 134 | + await generatePageMetaData(args); |
| 135 | + expect(args.actions.deletePage).toMatchInlineSnapshot(` |
| 136 | + [MockFunction] { |
| 137 | + "calls": [ |
| 138 | + [ |
| 139 | + { |
| 140 | + "context": { |
| 141 | + "locale": "en-US", |
| 142 | + "localePagesId": "index", |
| 143 | + }, |
| 144 | + "isCreatedByStatefulCreatePages": true, |
| 145 | + }, |
| 146 | + ], |
| 147 | + ], |
| 148 | + "results": [ |
| 149 | + { |
| 150 | + "type": "return", |
| 151 | + "value": undefined, |
| 152 | + }, |
| 153 | + ], |
| 154 | + } |
| 155 | + `); |
| 156 | + expect(args.actions.createPage).toMatchInlineSnapshot(` |
| 157 | + [MockFunction] { |
| 158 | + "calls": [ |
| 159 | + [ |
| 160 | + { |
| 161 | + "context": { |
| 162 | + "locale": "en-US", |
| 163 | + "localePagesId": "index", |
| 164 | + "metaData": { |
| 165 | + "description": "Companions for adventures in the world of bits and bytes.", |
| 166 | + "title": "Home - openscript Ltd.", |
| 167 | + }, |
| 168 | + }, |
| 169 | + "isCreatedByStatefulCreatePages": true, |
| 170 | + }, |
| 171 | + ], |
| 172 | + ], |
| 173 | + "results": [ |
| 174 | + { |
| 175 | + "type": "return", |
| 176 | + "value": undefined, |
| 177 | + }, |
| 178 | + ], |
| 179 | + } |
| 180 | + `); |
| 181 | + }); |
| 182 | +}); |
0 commit comments