import { HttpModule } from '@nestjs/axios'; import { Test, TestingModule } from '@nestjs/testing'; import * as _ from 'lodash'; import { ConfigurationModule } from '../configuration/configuration.module'; import { TagEnum } from './enums/tag.enum'; import { PostsService } from './posts.service'; import { Post } from './schemas/post.schema'; import { Tag } from './schemas/tag.schema'; describe('PostsService', () => { let postsService: PostsService; // let configService: ConfigurationServiceMock; const locationtags = [ { id: '61b74b2c0b9800018ca3df', name: 'oullins', slug: 'oullins', description: 'commune', feature_image: null, visibility: 'public', og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, codeinjection_head: null, codeinjection_foot: null, canonical_url: null, accent_color: null, created_at: '2021-12-13T13:31:24.000Z', updated_at: '2021-12-13T13:31:25.000Z', url: 'http://localhost:2368/tag/oullins/', }, { id: '61b74b2c0d9800018ca3df', name: 'oullins', slug: 'oullins', description: 'commune', feature_image: null, visibility: 'public', og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, codeinjection_head: null, codeinjection_foot: null, canonical_url: null, accent_color: null, created_at: '2021-12-13T13:31:24.000Z', updated_at: '2021-12-13T13:31:25.000Z', url: 'http://localhost:2368/tag/oullins/', }, { id: '61b74b2d3b9800018ca3e0', name: 'Bron', slug: 'bron', description: 'commune', feature_image: null, visibility: 'public', og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, codeinjection_head: null, codeinjection_foot: null, canonical_url: null, accent_color: null, created_at: '2021-12-13T13:31:24.000Z', updated_at: '2021-12-13T13:31:24.000Z', url: 'http://localhost:2368/404/', }, ]; const publictags = [ { id: '61b74b2d0d3b9800018ca3f6', name: 'Séniors (+ de 65ans)', slug: 'seniors-de-65ans', description: 'public', feature_image: null, visibility: 'public', og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, codeinjection_head: null, codeinjection_foot: null, canonical_url: null, accent_color: null, created_at: '2021-12-13T13:31:25.000Z', updated_at: '2021-12-13T13:31:25.000Z', url: 'http://localhost:2368/404/', }, { id: '61b74b2d0d3b98000ca3f7', name: 'Allophones', slug: 'allophones', description: 'public', feature_image: null, visibility: 'public', og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, codeinjection_head: null, codeinjection_foot: null, canonical_url: null, accent_color: null, created_at: '2021-12-13T13:31:25.000Z', updated_at: '2021-12-13T13:31:25.000Z', url: 'http://localhost:2368/404/', }, ]; const otherTags = [ { id: '61b74b2d0d3b980001a3fa', name: 'Études', slug: 'etudes', description: null, feature_image: null, visibility: 'public', og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, codeinjection_head: null, codeinjection_foot: null, canonical_url: null, accent_color: null, created_at: '2021-12-13T13:31:25.000Z', updated_at: '2021-12-13T13:31:25.000Z', url: 'http://localhost:2368/tag/etudes/', }, { id: '61b74b2d0d3b980001a3fa', name: 'A la une', slug: 'a-la-une', description: null, feature_image: null, visibility: 'public', og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, codeinjection_head: null, codeinjection_foot: null, canonical_url: null, accent_color: null, created_at: '2021-12-13T13:31:25.000Z', updated_at: '2021-12-13T13:31:25.000Z', url: 'http://localhost:2368/tag/a-la-une/', }, { id: '61b74b2d0d3b980001a3fa', name: 'Infos', slug: 'infos', description: null, feature_image: null, visibility: 'public', og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, codeinjection_head: null, codeinjection_foot: null, canonical_url: null, accent_color: null, created_at: '2021-12-13T13:31:25.000Z', updated_at: '2021-12-13T13:31:25.000Z', url: 'http://localhost:2368/tag/infos/', }, ]; const tagsData = [...locationtags, ...publictags, ...otherTags]; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [ConfigurationModule, HttpModule], providers: [PostsService], }).compile(); postsService = module.get<PostsService>(PostsService); jest.spyOn(postsService, 'getTags').mockImplementationOnce(async (): Promise<Tag[]> => tagsData); }); it('should be defined', () => { expect(postsService).toBeDefined(); }); it('should get tags', async () => { expect(await postsService.getTags()).toEqual(tagsData); }); it('should get location tags', async () => { expect(await postsService.getLocationTags()).toEqual(locationtags); }); it('should get public tags', async () => { expect(await postsService.getPublicTags()).toEqual(publictags); }); it('should get regular tags', async () => { expect(await postsService.getRegularTags()).toEqual([otherTags[1], otherTags[2], otherTags[0]]); }); describe('arraymove', () => { it('should order tags for display: A La Une tag should be set to first index', () => { const dataCopy = [...otherTags]; postsService.arraymove(dataCopy, _.findIndex(dataCopy, { slug: TagEnum.aLaUne }), 0); expect(dataCopy).toEqual([otherTags[1], otherTags[0], otherTags[2]]); }); it('should order tags for display: Info tag should be set to second index', () => { const dataCopy = [...otherTags]; expect(postsService.arraymove(dataCopy, _.findIndex(dataCopy, { slug: TagEnum.infos }), 1)).toEqual([ otherTags[0], otherTags[2], otherTags[1], ]); }); it('should order tags for display: A La Une tag should be set to first index and Info to second', () => { const dataCopy = [...otherTags]; postsService.arraymove(dataCopy, _.findIndex(dataCopy, { slug: TagEnum.aLaUne }), 0); expect(postsService.arraymove(dataCopy, _.findIndex(dataCopy, { slug: TagEnum.infos }), 1)).toEqual([ otherTags[1], otherTags[2], otherTags[0], ]); }); }); describe('formatPosts', () => { const postToFormat: Post = { id: '61c4847b0ff4550001505090', uuid: 'f4ee5a37-a343-4cad-8a32-3f6cf87f9569', title: 'Only feature image', slug: 'only-feature-image', html: '<p>Test</p>', comment_id: '61c4847b0ff4550001505090', feature_image: 'http://localhost:2368/content/images/2021/12/dacc-4.png', featured: false, visibility: 'public', email_recipient_filter: 'none', created_at: '2021-12-23T14:15:23.000+00:00', updated_at: '2021-12-23T14:15:45.000+00:00', published_at: '2021-12-23T14:15:45.000+00:00', custom_excerpt: null, codeinjection_head: null, codeinjection_foot: null, custom_template: null, canonical_url: null, tags: [], authors: [], primary_author: [], primary_tag: [], url: 'http://localhost:2368/only-feature-image/', excerpt: '« Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus.\n' + 'Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed,\n' + 'dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper\n' + 'congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est\n' + 'eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu\n' + 'massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut\n' + 'in risus volutpat libero pharetra tem', reading_time: '2', access: true, send_email_when_published: false, og_image: null, og_title: null, og_description: null, twitter_image: null, twitter_title: null, twitter_description: null, meta_title: null, meta_description: null, email_subject: null, frontmatter: null, }; it('should format post with no custom expert', () => { const objCp = { ...postToFormat }; const result = { ...postToFormat }; result.excerpt = 'Inconnu'; result.feature_image = 'https://localhost/blog/content/images/2021/12/dacc-4.png'; expect(postsService.formatPosts(objCp)).toEqual(result); }); it('should format post with custom expert', () => { const objCp = { ...postToFormat }; objCp.custom_excerpt = 'Toto'; const result = { ...postToFormat }; result.custom_excerpt = 'Toto'; result.feature_image = 'https://localhost/blog/content/images/2021/12/dacc-4.png'; expect(postsService.formatPosts(objCp)).toEqual(result); }); }); });