|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import moment from 'moment'; |
| 10 | +import { omit } from 'lodash'; |
| 11 | +import { validateIntegrity, validatePublishedDate, convertItem } from './convert_items'; |
| 12 | +import type { ApiItem, NewsfeedItem } from '../types'; |
| 13 | + |
| 14 | +const createApiItem = (parts: Partial<ApiItem> = {}): ApiItem => ({ |
| 15 | + hash: 'hash', |
| 16 | + expire_on: new Date(), |
| 17 | + publish_on: new Date(), |
| 18 | + title: {}, |
| 19 | + description: {}, |
| 20 | + link_url: {}, |
| 21 | + ...parts, |
| 22 | +}); |
| 23 | + |
| 24 | +const createNewsfeedItem = (parts: Partial<NewsfeedItem> = {}): NewsfeedItem => ({ |
| 25 | + title: 'title', |
| 26 | + description: 'description', |
| 27 | + linkText: 'linkText', |
| 28 | + linkUrl: 'linkUrl', |
| 29 | + badge: 'badge', |
| 30 | + publishOn: moment(), |
| 31 | + expireOn: moment(), |
| 32 | + hash: 'hash', |
| 33 | + ...parts, |
| 34 | +}); |
| 35 | + |
| 36 | +describe('convertItem', () => { |
| 37 | + const item = createApiItem({ |
| 38 | + languages: ['en', 'fr'], |
| 39 | + title: { |
| 40 | + en: 'en title', |
| 41 | + fr: 'fr title', |
| 42 | + }, |
| 43 | + description: { |
| 44 | + en: 'en desc', |
| 45 | + fr: 'fr desc', |
| 46 | + }, |
| 47 | + link_text: { |
| 48 | + en: 'en link text', |
| 49 | + fr: 'fr link text', |
| 50 | + }, |
| 51 | + link_url: { |
| 52 | + en: 'en link url', |
| 53 | + fr: 'fr link url', |
| 54 | + }, |
| 55 | + badge: { |
| 56 | + en: 'en badge', |
| 57 | + fr: 'fr badge', |
| 58 | + }, |
| 59 | + publish_on: new Date('2014-10-31T04:23:47Z'), |
| 60 | + expire_on: new Date('2049-10-31T04:23:47Z'), |
| 61 | + hash: 'hash', |
| 62 | + }); |
| 63 | + |
| 64 | + it('converts api items to newsfeed items using the specified language', () => { |
| 65 | + expect(convertItem(item, 'fr')).toMatchObject({ |
| 66 | + title: 'fr title', |
| 67 | + description: 'fr desc', |
| 68 | + linkText: 'fr link text', |
| 69 | + linkUrl: 'fr link url', |
| 70 | + badge: 'fr badge', |
| 71 | + hash: 'hash', |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('fallbacks to `en` is the language is not present', () => { |
| 76 | + expect(convertItem(item, 'de')).toMatchObject({ |
| 77 | + title: 'en title', |
| 78 | + description: 'en desc', |
| 79 | + linkText: 'en link text', |
| 80 | + linkUrl: 'en link url', |
| 81 | + badge: 'en badge', |
| 82 | + hash: 'hash', |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe('validatePublishedDate', () => { |
| 88 | + it('returns false when the publish date is not reached yet', () => { |
| 89 | + expect( |
| 90 | + validatePublishedDate( |
| 91 | + createApiItem({ |
| 92 | + publish_on: new Date('2055-10-31T04:23:47Z'), // too new |
| 93 | + expire_on: new Date('2056-10-31T04:23:47Z'), |
| 94 | + }) |
| 95 | + ) |
| 96 | + ).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it('returns false when the expire date is already reached', () => { |
| 100 | + expect( |
| 101 | + validatePublishedDate( |
| 102 | + createApiItem({ |
| 103 | + publish_on: new Date('2013-10-31T04:23:47Z'), |
| 104 | + expire_on: new Date('2014-10-31T04:23:47Z'), // too old |
| 105 | + }) |
| 106 | + ) |
| 107 | + ).toBe(false); |
| 108 | + }); |
| 109 | + |
| 110 | + it('returns true when current date is between the publish and expire dates', () => { |
| 111 | + expect( |
| 112 | + validatePublishedDate( |
| 113 | + createApiItem({ |
| 114 | + publish_on: new Date('2014-10-31T04:23:47Z'), |
| 115 | + expire_on: new Date('2049-10-31T04:23:47Z'), |
| 116 | + }) |
| 117 | + ) |
| 118 | + ).toBe(true); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('validateIntegrity', () => { |
| 123 | + it('returns false if `title` is missing', () => { |
| 124 | + expect(validateIntegrity(omit(createNewsfeedItem(), 'title'))).toBe(false); |
| 125 | + }); |
| 126 | + it('returns false if `description` is missing', () => { |
| 127 | + expect(validateIntegrity(omit(createNewsfeedItem(), 'description'))).toBe(false); |
| 128 | + }); |
| 129 | + it('returns false if `linkText` is missing', () => { |
| 130 | + expect(validateIntegrity(omit(createNewsfeedItem(), 'linkText'))).toBe(false); |
| 131 | + }); |
| 132 | + it('returns false if `linkUrl` is missing', () => { |
| 133 | + expect(validateIntegrity(omit(createNewsfeedItem(), 'linkUrl'))).toBe(false); |
| 134 | + }); |
| 135 | + it('returns false if `publishOn` is missing', () => { |
| 136 | + expect(validateIntegrity(omit(createNewsfeedItem(), 'publishOn'))).toBe(false); |
| 137 | + }); |
| 138 | + it('returns false if `hash` is missing', () => { |
| 139 | + expect(validateIntegrity(omit(createNewsfeedItem(), 'hash'))).toBe(false); |
| 140 | + }); |
| 141 | + it('returns true if all mandatory fields are present', () => { |
| 142 | + expect(validateIntegrity(createNewsfeedItem())).toBe(true); |
| 143 | + }); |
| 144 | +}); |
0 commit comments