import { HttpModule, HttpService } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { of } from 'rxjs'; import { ConfigurationModule } from '../configuration/configuration.module'; import { PostsController } from './posts.controller'; import { PostsService } from './posts.service'; import { AxiosResponse } from 'axios'; describe('PostsController', () => { let controller: PostsController; const httpServiceMock = { get: jest.fn(), }; const postServiceMock = { getLocationTags: jest.fn(), getPublicTags: jest.fn(), getRegularTags: jest.fn(), formatPosts: jest.fn(), }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [ConfigurationModule, HttpModule], providers: [ { provide: PostsService, useValue: postServiceMock, }, { provide: HttpService, useValue: httpServiceMock, }, ], controllers: [PostsController], }).compile(); controller = module.get<PostsController>(PostsController); }); it('should be defined', () => { expect(controller).toBeDefined(); }); describe('findAll', () => { it('should get all posts', async () => { const query = ''; const result: AxiosResponse = { data: { posts: [ { 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: [Array], authors: [Array], primary_author: [Object], primary_tag: [Object], 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, }, { id: '61c4847b0ff4550001505090', uuid: 'f4ee5a37-a343-4cad-8a32-3f6cf87f9569', title: 'Only test image', slug: 'only-test-image', html: '<p>Test 2</p>', comment_id: '61c4847b0ff4550001505090', feature_image: 'http://localhost:2368/content/images/2021/12/test.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: [Array], authors: [Array], primary_author: [Object], primary_tag: [Object], 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, }, { id: '61c4847b0ff4550001505090', uuid: 'f4ee5a37-a343-4cad-8a32-3f6cf87f9569', title: 'Only toto image', slug: 'only-toto-image', html: '<p>Test 3</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: [Array], authors: [Array], primary_author: [Object], primary_tag: [Object], 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, }, ], meta: { pagination: { page: 1, limit: 15, pages: 1, total: 2, next: null, prev: null } }, }, status: 200, statusText: 'OK', headers: {}, config: {}, }; jest.spyOn(httpServiceMock, 'get').mockImplementationOnce(() => of(result)); const response = await controller.findAll(query); expect(response.posts.length).toEqual(3); expect(response.meta.pagination.limit).toEqual(15); expect(response.meta.pagination.page).toEqual(1); expect(response.meta.pagination.total).toEqual(2); }); }); describe('findAllTags', () => { it('should get all tags', async () => { postServiceMock.getLocationTags.mockImplementationOnce(() => [ { id: '61b74b2c0d3b9800018ca3df', 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: '61b74b2c0d3b9800018ca3df', 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: '61b74b2c0d3b9800018ca3e0', 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/', }, ]); postServiceMock.getPublicTags.mockImplementationOnce(() => [ { 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: '61b74b2d0d3b9800018ca3f7', 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/', }, ]); postServiceMock.getRegularTags.mockImplementationOnce(() => [ { id: '61b74b2d0d3b9800018ca3fa', 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/', }, ]); const result = await controller.findAllTags(); expect(result.commune.length).toBe(3); expect(result.others.length).toBe(1); expect(result.public.length).toBe(2); }); }); describe('getPostbyId', () => { it('should get post Hello by id 61c4847b0ff4550001505090', async () => { const data = [ { id: '61c4847b0ff4550001505090', uuid: 'f4ee5a37-a343-4cad-8a32-3f6cf87f9569', title: 'Hello', slug: 'hello', 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: [Array], authors: [Array], primary_author: [Object], primary_tag: [Object], url: 'http://localhost:2368/hello/', 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, }, ]; const axiosResult: AxiosResponse = { data: { posts: data, }, status: 200, statusText: 'OK', headers: {}, config: {}, }; httpServiceMock.get.mockImplementationOnce(() => of(axiosResult)); postServiceMock.formatPosts.mockImplementationOnce(() => data); const result = await (await controller.getPostbyId('61c4847b0ff4550001505090')).toPromise(); expect(result).toStrictEqual({ posts: [data] }); }); }); });