Skip to content

Commit

Permalink
Add tests for accounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Dec 19, 2024
1 parent b9edb39 commit 9f3e9f1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,57 @@ describe('AccountDbService', () => {
});
});

describe('saveAll', () => {
describe('when given account that does not exist', () => {
const setup = () => {
const account = accountDoFactory.build({
id: undefined,
});
const savedAccount = accountDoFactory.build({
...account,
id: new ObjectId().toHexString(),
});

accountRepo.saveAll.mockResolvedValueOnce([savedAccount]);

return { account, savedAccount };
};

it('should save it', async () => {
const { account, savedAccount } = setup();

const result = await accountService.saveAll([account]);

expect(result.length).toBe(1);
expect(result[0]).toStrictEqual(savedAccount);
expect(accountRepo.saveAll).toHaveBeenCalledTimes(1);
});
});

describe('when given account that exist', () => {
const setup = () => {
const account = accountDoFactory.build();
const foundAccount = accountDoFactory.build();
const updateSpy = jest.spyOn(foundAccount, 'update');

accountRepo.findById.mockResolvedValueOnce(foundAccount);
accountRepo.saveAll.mockResolvedValueOnce([foundAccount]);

return { account, foundAccount, updateSpy };
};

it('should update it', async () => {
const { account, foundAccount, updateSpy } = setup();

const result = await accountService.saveAll([account]);

expect(updateSpy).toHaveBeenCalledTimes(1);
expect(result.length).toBe(1);
expect(result[0].id).toBe(foundAccount.id);
});
});
});

describe('updateUsername', () => {
describe('when updating username', () => {
const setup = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,30 @@ describe('AccountService', () => {
});
});

describe('saveAll', () => {
describe('when saving accounts', () => {
const setup = () => {
const accounts = accountDoFactory.buildList(1);

configService.get.mockReturnValue(true);
accountServiceDb.saveAll.mockResolvedValueOnce(accounts);
accountServiceIdm.saveAll.mockResolvedValueOnce(accounts);

return { accounts, sut: newAccountService() };
};

it('should delegate to db and idm service', async () => {
const { accounts, sut } = setup();

const result = await sut.saveAll(accounts);

expect(result).toBeDefined();
expect(accountServiceDb.saveAll).toHaveBeenCalledTimes(1);
expect(accountServiceIdm.saveAll).toHaveBeenCalledTimes(1);
});
});
});

describe('saveWithValidation', () => {
describe('When calling with an empty username', () => {
it('should throw an ValidationError', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ describe('account repo', () => {
});
});

describe('saveAll', () => {
describe('When multiple accounts are given', () => {
const setup = () => {
const accounts = accountDoFactory.buildList(3);
const accountIds = accounts.map((account) => account.id);

return { accounts, accountIds };
};

it('should save all accounts', async () => {
const { accounts, accountIds } = setup();

await repo.saveAll(accounts);

const foundAccounts = await em.find(AccountEntity, { id: { $in: accountIds } });
expect(foundAccounts.length).toBe(accounts.length);
});
});
});

describe('findById', () => {
describe('When the account exists', () => {
const setup = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AccountScope } from './scope/account-scope';
export class AccountRepo {
constructor(private readonly em: EntityManager) {}

get entityName() {
get entityName(): typeof AccountEntity {
return AccountEntity;
}

Expand Down Expand Up @@ -93,7 +93,7 @@ export class AccountRepo {
return AccountEntityToDoMapper.mapToDo(entity);
}

getObjectReference<Entity extends AnyEntity<Entity>>(
public getObjectReference<Entity extends AnyEntity<Entity>>(
entityName: EntityName<Entity>,
id: Primary<Entity> | Primary<Entity>[]
): Entity {
Expand Down Expand Up @@ -186,7 +186,7 @@ export class AccountRepo {
return AccountEntityToDoMapper.mapEntitiesToDos(result);
}

async findByUserIdsAndSystemId(userIds: string[], systemId: string): Promise<string[]> {
public async findByUserIdsAndSystemId(userIds: string[], systemId: string): Promise<string[]> {
const scope = new AccountScope();
const userIdScope = new AccountScope();

Expand Down

0 comments on commit 9f3e9f1

Please sign in to comment.