Skip to content

Commit

Permalink
refactor: use generated types and add CustomViewInstallation model
Browse files Browse the repository at this point in the history
  • Loading branch information
kark committed Dec 1, 2023
1 parent f3b94c9 commit 0a71416
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { TBuilder } from '@commercetools-test-data/core';
import type { TMcSettingsCustomViewInstallationPermission } from '../../../../graphql-types';

export type TCustomViewInstallationPermission = {
name: string;
oAuthScopes: string[];
};
export type TCustomViewInstallationPermissionGraphql =
TCustomViewInstallationPermission & {
__typename: 'CustomViewInstallationPermission';
};
TMcSettingsCustomViewInstallationPermission;

export type TCustomViewInstallationPermission = Omit<
TCustomViewInstallationPermissionGraphql,
'__typename'
>;

export type TCustomViewInstallationPermissionBuilder =
TBuilder<TCustomViewInstallationPermission>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type {
TCustomViewInstallation,
TCustomViewInstallationGraphql,
} from './types';
import * as CustomViewInstallationModel from './index';

describe('builder', () => {
it(
...createBuilderSpec<TCustomViewInstallation, TCustomViewInstallation>(
'default',
CustomViewInstallationModel.random(),
expect.objectContaining({
id: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
acceptedPermissions: expect.arrayContaining([
expect.objectContaining({
name: expect.stringMatching(/^(view|manage)$/),
oAuthScopes: expect.arrayContaining([expect.any(String)]),
}),
]),
installInAllProjects: expect.any(Boolean),
projects: expect.any(Array),
ownerId: expect.any(String),
owner: expect.any(Object),
})
)
);
it(
...createBuilderSpec<
TCustomViewInstallation,
TCustomViewInstallationGraphql
>(
'graphql',
CustomViewInstallationModel.random(),
expect.objectContaining({
__typename: 'CustomViewInstallation',
id: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
acceptedPermissions: expect.arrayContaining([
expect.objectContaining({
name: expect.stringMatching(/^(view|manage)$/),
oAuthScopes: expect.arrayContaining([expect.any(String)]),
}),
]),
installInAllProjects: expect.any(Boolean),
projects: expect.any(Array),
ownerId: expect.any(String),
owner: expect.any(Object),
})
)
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Builder } from '@commercetools-test-data/core';
import generator from './generator';
import transformers from './transformers';
import type {
TCustomViewInstallation,
TCreateCustomViewInstallationBuilder,
} from './types';

const Model: TCreateCustomViewInstallationBuilder = () =>
Builder<TCustomViewInstallation>({
generator,
transformers,
});

export default Model;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { fake, Generator } from '@commercetools-test-data/core';
import { createRelatedDates } from '@commercetools-test-data/utils';
import * as CustomViewInstallationPermission from '../../custom-view-installation-permission';
import type { TCustomViewInstallation } from './types';

const [getOlderDate, getNewerDate] = createRelatedDates();

const generator = Generator<TCustomViewInstallation>({
fields: {
id: fake((f) => f.string.uuid()),
createdAt: fake(getOlderDate),
updatedAt: fake(getNewerDate),
acceptedPermissions: fake(() => [
CustomViewInstallationPermission.presets.ViewOnlyPermissions(),
CustomViewInstallationPermission.presets.ManageOnlyPermissions(),
]),
projects: fake(() => []),
installInAllProjects: fake((f) => f.datatype.boolean()),
owner: fake(() => ({})),
ownerId: fake((f) => f.string.uuid()),
},
});

export default generator;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as random } from './builder';
export * as presets from './presets';
export * from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const presets = {};

export default presets;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Transformer } from '@commercetools-test-data/core';
import type {
TCustomViewInstallation,
TCustomViewInstallationGraphql,
} from './types';

const transformers = {
default: Transformer<TCustomViewInstallation, TCustomViewInstallation>(
'default',
{
buildFields: ['acceptedPermissions', 'projects'],
}
),
graphql: Transformer<TCustomViewInstallation, TCustomViewInstallationGraphql>(
'graphql',
{
buildFields: ['acceptedPermissions', 'projects'],
addFields: () => ({
__typename: 'CustomViewInstallation',
}),
}
),
};

export default transformers;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { TBuilder } from '@commercetools-test-data/core';
import type { TMcSettingsCustomViewInstallation } from '../../../../../graphql-types';

export type TCustomViewInstallationGraphql = TMcSettingsCustomViewInstallation;

export type TCustomViewInstallation = Omit<
TCustomViewInstallationGraphql,
'__typename'
>;

export type TCustomViewInstallationBuilder = TBuilder<TCustomViewInstallation>;
export type TCreateCustomViewInstallationBuilder =
() => TCustomViewInstallationBuilder;
1 change: 1 addition & 0 deletions models/custom-view/src/custom-view-installation/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Export models
export * as CustomViewInstallation from './custom-view-installation';
export * as RestrictedCustomViewInstallationForOrganization from './restricted-custom-view-installation-for-organization';
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import type { TBuilder } from '@commercetools-test-data/core';
import type { TCustomView } from '../../custom-view';
import type { TCustomViewInstallationPermission } from '../../custom-view-installation-permission/types';

// TODO: generate graphql types and use those
type ProjectExtension = {
projectKey: string;
};

export type TRestrictedCustomViewInstallationForOrganization = {
acceptedPermissions: TCustomViewInstallationPermission[];
createdAt: string;
customView: TCustomView;
id: string;
installInAllProjects: boolean;
projects: ProjectExtension[];
updatedAt: string;
};
import type { TMcSettingsRestrictedCustomViewInstallationForOrganization } from '../../../../../graphql-types';

export type TRestrictedCustomViewInstallationForOrganizationGraphql =
TRestrictedCustomViewInstallationForOrganization & {
__typename: 'RestrictedCustomViewInstallationForOrganization';
};
TMcSettingsRestrictedCustomViewInstallationForOrganization;

export type TRestrictedCustomViewInstallationForOrganization = Omit<
TMcSettingsRestrictedCustomViewInstallationForOrganization,
'__typename'
>;

export type TRestrictedCustomViewInstallationForOrganizationBuilder =
TBuilder<TRestrictedCustomViewInstallationForOrganization>;
Expand Down
1 change: 1 addition & 0 deletions models/custom-view/src/custom-view-installation/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Export types
export * from './custom-view-installation/types';
export * from './restricted-custom-view-installation-for-organization/types';
19 changes: 0 additions & 19 deletions models/custom-view/src/custom-view/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,6 @@ type CustomViewPermission = {
oAuthScopes: string[];
};

export type CustomViewInstallationPermission = {
createdAt: string;
id: string;
name: string;
oAuthScopes: string[];
updatedAt: string;
};

export type CustomViewInstallation = {
acceptedPermissions: CustomViewInstallationPermission[];
createdAt: string;
id: string;
installInAllProjects: boolean;
// owner: OrganizationExtension;
ownerId: string;
// projects: ProjectExtension[];
updatedAt: string;
};

export type TCustomView = {
id: string;
// owner: OrganizationExtension;
Expand Down
5 changes: 4 additions & 1 deletion models/custom-view/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ export * as CustomViewPermission from './custom-view-permission';
export * as CustomViewPermissionDraft from './custom-view-permission-draft';

export * as CustomViewTypeSettingsForCustomPanel from './custom-view-type-settings-for-custom-panel';
export { RestrictedCustomViewInstallationForOrganization } from './custom-view-installation';
export {
RestrictedCustomViewInstallationForOrganization,
CustomViewInstallation,
} from './custom-view-installation';
export * as CustomViewInstallationPermission from './custom-view-installation-permission';

0 comments on commit 0a71416

Please sign in to comment.