Skip to content

Commit

Permalink
fix: Implemented WebAPI.create and delete
Browse files Browse the repository at this point in the history
Refactored and documented metadata db.
  • Loading branch information
BetimBeja committed Feb 12, 2023
1 parent 8552ffd commit 9374860
Show file tree
Hide file tree
Showing 9 changed files with 2,518 additions and 122 deletions.
66 changes: 59 additions & 7 deletions ComponentFramework-Mock/__tests__/WebApiMock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IInputs, IOutputs } from '../__sample-components__/OwnerLookup/generate
import userMetadataJson from './data/systemUser.json';
import userDataJson from './data/systemUserData.json';
import betimBeja from './data/betimBeja.json';
import { assert } from 'console';

describe('WebApiMock', () => {
let mockGenerator: ComponentFrameworkMockGenerator<IInputs, IOutputs>;
Expand All @@ -27,15 +28,15 @@ describe('WebApiMock', () => {
mockGenerator.metadata.initItems(JSON.parse(JSON.stringify(userDataJson)));
});

it('Should reject if no metadata', async () => {
it('Should reject retrieve if no metadata', async () => {
await expect(
mockGenerator.context.webAPI.retrieveRecord('account', '48268759-f226-ed11-9db1-000d3a264915'),
).rejects.toEqual({
message: 'Entity account does not exist.',
});
});

it('Should reject if row not found', async () => {
it('Should reject retrieve if row not found', async () => {
await expect(
mockGenerator.context.webAPI.retrieveRecord('systemuser', '48268759-f226-ed11-9db2-000d3a264915'),
).rejects.toEqual({
Expand All @@ -44,18 +45,69 @@ describe('WebApiMock', () => {
});

it('Should retrieve record when row is found', async () => {
delete betimBeja["@odata.etag"];
delete betimBeja['@odata.etag'];

await expect(
mockGenerator.context.webAPI.retrieveRecord('systemuser', '682d1eb3-0ba4-ed11-aad1-000d3add5311'),
).resolves.toEqual(JSON.parse(JSON.stringify(betimBeja)));
});

it('Should retrieve records selected attributes', async () => {
const {fullname} = JSON.parse(JSON.stringify(betimBeja));

const { fullname } = JSON.parse(JSON.stringify(betimBeja));

await expect(
mockGenerator.context.webAPI.retrieveRecord(
'systemuser',
'682d1eb3-0ba4-ed11-aad1-000d3add5311',
'?$select=fullname',
),
).resolves.toEqual({ fullname });
});

it('Should reject delete if no metadata', async () => {
await expect(
mockGenerator.context.webAPI.deleteRecord('account', '48268759-f226-ed11-9db1-000d3a264915'),
).rejects.toEqual({
message: 'Entity account does not exist.',
});
});

it('Should reject delete if row not found', async () => {
await expect(
mockGenerator.context.webAPI.retrieveRecord('systemuser', '682d1eb3-0ba4-ed11-aad1-000d3add5311','?$select=fullname'),
).resolves.toEqual({fullname});
mockGenerator.context.webAPI.deleteRecord('systemuser', '48268759-f226-ed11-9db2-000d3a264915'),
).rejects.toEqual({
message: "Could not find record with id: '48268759-f226-ed11-9db2-000d3a264915' for entity: 'systemuser'.",
});
});

it('Should delete record when row is found', async () => {
delete betimBeja['@odata.etag'];

await expect(
mockGenerator.context.webAPI.deleteRecord('systemuser', '682d1eb3-0ba4-ed11-aad1-000d3add5311'),
).resolves.toEqual({
name: 'Betim Beja',
id: '682d1eb3-0ba4-ed11-aad1-000d3add5311',
entityType: 'systemuser',
});
});

it('Should reject create if no metadata', async () => {
await expect(
mockGenerator.context.webAPI.createRecord('account', { accountid: '48268759-f226-ed11-9db1-000d3a264915' }),
).rejects.toEqual({
message: 'Entity account does not exist.',
});
});

it('Should create if metadata available', async () => {
const createdUser = await mockGenerator.context.webAPI.createRecord('systemuser', {
firstname: 'Betim',
lastname: 'Beja',
});
expect(createdUser.id).not.toBeNull();
expect(createdUser.id).not.toBeUndefined();
const userData = mockGenerator.metadata.GetRow('systemuser', createdUser.id);
expect(userData.row['firstname']).toEqual('Betim');
});
});
Loading

0 comments on commit 9374860

Please sign in to comment.