Untitled

mail@pastecode.io avatar
unknown
typescript
a year ago
4.7 kB
2
Indexable
Never
import { getApolloServer } from '@nestjs/apollo';
import { INestApplication } from '@nestjs/common';
import { TestingModule } from '@nestjs/testing';
import { ApolloServerBase } from 'apollo-server-core';
import { print } from 'graphql';

import {
  CREATE_ANNOUNCEMENT,
  UPDATE_ANNOUNCEMENT,
  DELETE_ANNOUNCEMENT,
} from '../../__operations__/mutations/announcement.mutation';
import {
  GET_ANNOUNCEMENTS,
  GET_UNREAD_ANNOUNCEMENTS,
} from '../../__operations__/queries/announcement.query';
import { getTestModule } from '../../testing/context.mock';
import { setTestPolicies } from '../../testing/db.getters';
import { FORBIDDEN_MESSAGE } from '../../utils/errors';
import {
  AnnouncementInput,
  AnnouncementUpdateInput,
} from '../../graphql.schema';

describe('AnnouncementsResolver', () => {
  let module: TestingModule;
  let app: INestApplication;
  let server: ApolloServerBase;

  beforeEach(async () => {
    module = await getTestModule();
    app = module.createNestApplication();
    await app.init();
    server = getApolloServer(module);
  });

  afterAll(async () => {
    await module.close();
  });

  it('can get announcements', async () => {
    await setTestPolicies(Subject.announcement, Action.read);

    const response = await server.executeOperation({
      query: print(GET_ANNOUNCEMENTS),
    });
    expect(response.errors).toBeUndefined();
    expect(response.data?.announcements.length).toBeGreaterThan(0);
  });

  it('can get unread announcements', async () => {
    await setTestPolicies(Subject.announcement, Action.read);
    const userId = 'someUserId';

    const response = await server.executeOperation({
      query: print(GET_UNREAD_ANNOUNCEMENTS),
      variables: { userId },
    });
    expect(response.errors).toBeUndefined();
    expect(response.data?.unreadAnnouncements.length).toBeGreaterThan(0);
  });

  it('can create announcement', async () => {
    await setTestPolicies(Subject.announcement, [Action.read, Action.create]);

    const data: AnnouncementInput = {
      title: 'New Announcement',
      description: 'This is a new announcement.',
      content: 'This is the content of the new announcement.',
    };

    const createResponse = await server.executeOperation({
      query: print(CREATE_ANNOUNCEMENT),
      variables: { data },
    });
    expect(createResponse.errors).toBeUndefined();
    const created = createResponse.data?.createAnnouncement;
    expect(created.id).toBeTruthy();
    expect(created.title).toEqual(data.title);
    expect(created.description).toEqual(data.description);
    expect(created.document.content).toEqual(data.content);
  });

  it('can update announcement', async () => {
    await setTestPolicies(Subject.announcement, Action.update);

    const existing = await prisma.announcement.findFirstOrThrow();

    const data: AnnouncementUpdateInput = {
      id: existing.id,
      title: 'Updated Announcement',
      description: 'This is an updated announcement.',
      content: 'This is the updated content of the announcement.',
    };

    const response = await server.executeOperation({
      query: print(UPDATE_ANNOUNCEMENT),
      variables: { data },
    });
    expect(response.errors).toBeUndefined();
    const updated = response.data?.updateAnnouncement;
    expect(updated.id).toBeTruthy();
    expect(updated.title).toEqual(data.title);
    expect(updated.description).toEqual(data.description);
    expect(updated.document.content).toEqual(data.content);
  });

  it('cannot delete announcement without permissions', async () => {
    await setTestPolicies(Subject.announcement, Action.update);
    const ids = ['shouldThrowAnyway'];
    const response = await server.executeOperation({
      query: print(DELETE_ANNOUNCEMENT),
      variables: { ids },
    });
    expect(response.errors![0].message).toEqual(FORBIDDEN_MESSAGE);
  });

  it('can delete announcement with permissions', async () => {
    await setTestPolicies(Subject.announcement, [Action.read, Action.delete]);

    const responseBefore = await server.executeOperation({
      query: print(GET_ANNOUNCEMENTS),
    });

    expect(responseBefore.errors).toBeUndefined();
    const prevAmount = responseBefore.data?.announcements.length;
    expect(prevAmount).toBeGreaterThan(0);

    const ids = [responseBefore.data?.announcements[0].id];

    const responseDelete = await server.executeOperation({
      query: print(DELETE_ANNOUNCEMENT),
      variables: { ids },
    });

    expect(responseDelete.errors).toBeUndefined();

    const responseAfter = await server.executeOperation({
      query: print(GET_ANNOUNCEMENTS),
    });

    expect(prevAmount - 1).toEqual(responseAfter.data?.announcements.length);
  });
});