'use strict';

const _ = require('lodash');
const roleService = require('../role');

describe('Role', () => {
  describe('create', () => {
    test('Creates a role', async () => {
      const dbCreate = jest.fn(role => Promise.resolve(role));

      global.strapi = {
        query: () => ({ create: dbCreate }),
      };

      const input = { name: 'super_admin', description: "Have all permissions. Can't be delete" };

      const createdRole = await roleService.create(input);

      expect(dbCreate).toHaveBeenCalledWith(input);
      expect(createdRole).toStrictEqual(input);
    });
  });
  describe('find one', () => {
    test('Finds a role', async () => {
      const role = {
        id: 1,
        name: 'super_admin',
        description: "Have all permissions. Can't be delete",
      };
      const dbFindOne = jest.fn(({ id }) => Promise.resolve(_.find([role], { id })));

      global.strapi = {
        query: () => ({ findOne: dbFindOne }),
      };

      const foundRole = await roleService.findOne({ id: role.id });

      expect(dbFindOne).toHaveBeenCalledWith({ id: role.id });
      expect(foundRole).toStrictEqual(role);
    });
    test('Finds all roles', async () => {
      const roles = [
        {
          id: 1,
          name: 'super_admin',
          description: "Have all permissions. Can't be delete",
        },
      ];
      const dbFind = jest.fn(() => Promise.resolve(roles));

      global.strapi = {
        query: () => ({ find: dbFind }),
      };

      const foundRoles = await roleService.findAll();

      expect(dbFind).toHaveBeenCalledWith({ _limit: -1 });
      expect(foundRoles).toStrictEqual(roles);
    });
  });
});

@@ -1145,24 +1145,516 @@
 e);%0A    %7D);%0A
+    test('Finds roles', async () =%3E %7B%0A      const roles = %5B%0A        %7B%0A          id: 1,%0A          name: 'super_admin',%0A          description: %22Have all permissions. Can't be delete%22,%0A        %7D,%0A      %5D;%0A      const dbFind = jest.fn(() =%3E Promise.resolve(roles));%0A%0A      global.strapi = %7B%0A        query: () =%3E (%7B find: dbFind %7D),%0A      %7D;%0A%0A      const foundRoles = await roleService.find();%0A%0A      expect(dbFind).toHaveBeenCalledWith(%7B%7D);%0A      expect(foundRoles).toStrictEqual(roles);%0A    %7D);%0A
     test('Fi

add test for service find