Skip to content

Commit

Permalink
refactor: use generate types for custom view (#591)
Browse files Browse the repository at this point in the history
* refactor: use generate types for custom view and include custom application installation in custom view

* refactor: update test for type-settings

* Update .changeset/lovely-waves-notice.md

Co-authored-by: Kacper Krzywiec <[email protected]>

---------

Co-authored-by: Kacper Krzywiec <[email protected]>
  • Loading branch information
Rhotimee and kark authored May 27, 2024
1 parent a5c62fb commit ac42d1b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 58 deletions.
6 changes: 6 additions & 0 deletions .changeset/lovely-waves-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@commercetools-test-data/custom-view': patch
---

- Use generated types for custom view
- Include custom view installation in custom view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import { TMcSettingsCustomViewSize } from '@commercetools-test-data/graphql-types';
import type {
TCustomViewTypeSettingsForCustomPanel,
TCustomViewTypeSettingsForCustomPanelGraphql,
Expand All @@ -27,7 +28,7 @@ describe('builder', () => {
TCustomViewTypeSettingsForCustomPanelGraphql
>(
'graphql',
CustomViewTypeSettings.random().size('SMALL'),
CustomViewTypeSettings.random().size(TMcSettingsCustomViewSize.Small),
expect.objectContaining({
__typename: 'CustomViewTypeSettings',
size: 'SMALL',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { TBuilder } from '@commercetools-test-data/core';
import type { TMcSettingsCustomViewTypeSettings } from '@commercetools-test-data/graphql-types';

export type CustomViewSize = 'SMALL' | 'LARGE';

export type TCustomViewTypeSettingsForCustomPanel = {
size: CustomViewSize;
};
export type TCustomViewTypeSettingsForCustomPanelGraphql =
TCustomViewTypeSettingsForCustomPanel & {
__typename: 'CustomViewTypeSettings';
};
TMcSettingsCustomViewTypeSettings;

export type TCustomViewTypeSettingsForCustomPanel = Omit<
TCustomViewTypeSettingsForCustomPanelGraphql,
'__typename'
>;

export type TCustomViewTypeSettingsForCustomPanelBuilder =
TBuilder<TCustomViewTypeSettingsForCustomPanel>;
Expand Down
35 changes: 35 additions & 0 deletions models/custom-view/src/custom-view/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ describe('builder', () => {
size: expect.stringMatching(/^(SMALL|LARGE)$/),
}),
url: expect.any(String),
installedBy: expect.arrayContaining([
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),
}),
]),
})
)
);
Expand Down Expand Up @@ -67,6 +84,24 @@ describe('builder', () => {
size: expect.stringMatching(/^(SMALL|LARGE)$/),
}),
url: expect.any(String),
installedBy: expect.arrayContaining([
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),
}),
]),
})
)
);
Expand Down
6 changes: 4 additions & 2 deletions models/custom-view/src/custom-view/generator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { LocalizedField } from '@commercetools-test-data/commons';
import { fake, Generator } from '@commercetools-test-data/core';
import { createRelatedDates } from '@commercetools-test-data/utils';
import { CustomViewInstallation } from '../custom-view-installation';
import * as CustomViewPermission from '../custom-view-permission';
import * as CustomViewTypeSettingsForCustomPanel from '../custom-view-type-settings-for-custom-panel';
import { defaultLocators } from './constants';
import { TCustomView } from './types';
import type { TCustomView } from './types';

const [getOlderDate, getNewerDate] = createRelatedDates();

Expand All @@ -28,7 +29,8 @@ const generator = Generator<TCustomView>({
]),
status: fake((f) => f.helpers.arrayElement(['DRAFT', 'PRIVATE_USAGE'])),
type: fake((f) => f.helpers.arrayElement(['CustomPanel'])),
typeSettings: fake((f) => CustomViewTypeSettingsForCustomPanel.random()),
typeSettings: fake(() => CustomViewTypeSettingsForCustomPanel.random()),
installedBy: fake(() => [CustomViewInstallation.random()]),
createdAt: fake(getOlderDate),
updatedAt: fake(getNewerDate),
},
Expand Down
14 changes: 12 additions & 2 deletions models/custom-view/src/custom-view/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import type { TCustomView, TCustomViewGraphql } from './types';

const transformers = {
default: Transformer<TCustomView, TCustomView>('default', {
buildFields: ['labelAllLocales', 'permissions', 'typeSettings'],
buildFields: [
'labelAllLocales',
'permissions',
'typeSettings',
'installedBy',
],
}),
graphql: Transformer<TCustomView, TCustomViewGraphql>('graphql', {
buildFields: ['labelAllLocales', 'permissions', 'typeSettings'],
buildFields: [
'labelAllLocales',
'permissions',
'typeSettings',
'installedBy',
],
addFields: () => ({
__typename: 'CustomView',
}),
Expand Down
53 changes: 8 additions & 45 deletions models/custom-view/src/custom-view/types.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,16 @@
import { TBuilder } from '@commercetools-test-data/core';
import type {
TMcSettingsCustomView,
TMcSettingsCustomViewDraftDataInput,
} from '@commercetools-test-data/graphql-types';

// TODO: generate graphql types and use those
export type TCustomViewGraphql = TMcSettingsCustomView;

type LocalizedField = {
id?: string;
locale: string;
value?: string;
};
export type TCustomView = Omit<TCustomViewGraphql, '__typename' | 'owner'>;

type CustomViewType = 'CustomPanel';
export type TCustomViewDraftGraphql = TMcSettingsCustomViewDraftDataInput;

type CustomViewSize = 'SMALL' | 'LARGE';

type CustomViewStatus = 'DRAFT' | 'PRIVATE_USAGE';

type CustomViewTypeSettings = {
size?: CustomViewSize;
};

type CustomViewPermission = {
name: string;
oAuthScopes: string[];
};

export type TCustomView = {
id: string;
// owner: OrganizationExtension;
ownerId: string;
url: string;
defaultLabel: string;
description?: string;
labelAllLocales: LocalizedField[];
locators: string[];
permissions: CustomViewPermission[];
status: CustomViewStatus;
type: CustomViewType;
typeSettings?: CustomViewTypeSettings;
createdAt: string;
updatedAt: string;
};
export type TCustomViewGraphql = TCustomView & {
__typename: 'CustomView';
};

export type TCustomViewDraft = Omit<
TCustomView,
'id' | 'createdAt' | 'updatedAt' | 'ownerId' | 'status'
>;
export type TCustomViewDraftGraphql = TCustomViewDraft;
export type TCustomViewDraft = Omit<TCustomViewDraftGraphql, '__typename'>;

export type TCustomViewBuilder = TBuilder<TCustomView>;
export type TCreateCustomViewBuilder = () => TCustomViewBuilder;
Expand Down

0 comments on commit ac42d1b

Please sign in to comment.