Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PANGOLIN-3185 - New package for model: State #440

Merged
merged 10 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions models/state/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) commercetools GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions models/state/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# @commercetools-test-data/state

This package provides data models for the commercetools platform `State` representations.

https://docs.commercetools.com/api/projects/states#representations

# Install

```bash
$ pnpm add -D @commercetools-test-data/state
```

# Usage

```ts
import {
State,
StateDraft,
type TState,
type TStateDraft,
} from '@commercetools-test-data/state';

const state = State.random().build<TState>();
const StateDraft = StateDraft.random().build<TStateDraft>();
```
37 changes: 37 additions & 0 deletions models/state/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@commercetools-test-data/state",
"version": "6.4.1",
"description": "Data model for commercetools API State",
"bugs": "https://github.com/commercetools/test-data/issues",
"repository": {
"type": "git",
"url": "https://github.com/commercetools/test-data.git",
"directory": "models/state"
},
"keywords": [
"javascript",
"typescript",
"test-data"
],
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "dist/commercetools-test-data-state.cjs.js",
"module": "dist/commercetools-test-data-state.esm.js",
"files": [
"dist",
"package.json",
"LICENSE",
"README.md"
],
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime-corejs3": "^7.17.9",
"@commercetools-test-data/commons": "6.4.1",
"@commercetools-test-data/core": "6.4.1",
"@commercetools-test-data/utils": "6.4.1",
"@commercetools/platform-sdk": "^6.0.0",
"@faker-js/faker": "^8.0.0"
}
}
121 changes: 121 additions & 0 deletions models/state/src/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */

import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type { TState, TStateGraphql } from './types';
import * as State from './index';
import { roles } from './constants';

describe('builder', () => {
it(
...createBuilderSpec<TState, TState>(
'default',
State.random(),
expect.objectContaining({
id: expect.any(String),
version: expect.any(Number),
key: expect.any(String),
type: expect.any(String),
name: expect.objectContaining({
de: expect.any(String),
en: expect.any(String),
fr: expect.any(String),
}),
description: expect.objectContaining({
en: expect.any(String),
}),
initial: expect.any(Boolean),
builtIn: expect.any(Boolean),
roles: expect.any(Array),
transitions: null,
createdAt: expect.any(String),
createdBy: expect.objectContaining({
customer: expect.objectContaining({ typeId: 'customer' }),
}),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.objectContaining({
customer: expect.objectContaining({ typeId: 'customer' }),
}),
})
)
);
it(
...createBuilderSpec<TState, TState>(
'rest',
State.random(),
expect.objectContaining({
id: expect.any(String),
version: expect.any(Number),
key: expect.any(String),
type: expect.any(String),
name: expect.objectContaining({
de: expect.any(String),
en: expect.any(String),
fr: expect.any(String),
}),
description: expect.objectContaining({
en: expect.any(String),
}),
initial: expect.any(Boolean),
builtIn: expect.any(Boolean),
roles: expect.any(Array),
transitions: null,
createdAt: expect.any(String),
createdBy: expect.objectContaining({
customer: expect.objectContaining({ typeId: 'customer' }),
}),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.objectContaining({
customer: expect.objectContaining({ typeId: 'customer' }),
}),
})
)
);
// Note that the State graphql is provided as scaffolding only and may not be complete at this time.
it(
...createBuilderSpec<TState, TStateGraphql>(
'graphql',
State.random(),
expect.objectContaining({
id: expect.any(String),
version: expect.any(Number),
key: expect.any(String),
type: expect.any(String),
name: expect.arrayContaining([
expect.objectContaining({
locale: 'en',
value: expect.any(String),
}),
expect.objectContaining({
locale: 'de',
value: expect.any(String),
}),
expect.objectContaining({
locale: 'fr',
value: expect.any(String),
}),
]),
description: expect.arrayContaining([
expect.objectContaining({
locale: 'en',
value: expect.any(String),
}),
]),
initial: expect.any(Boolean),
builtIn: expect.any(Boolean),
roles: expect.any(Array),
transitions: null,
createdAt: expect.any(String),
createdBy: expect.objectContaining({
customerRef: expect.objectContaining({ typeId: 'customer' }),
userRef: expect.objectContaining({ typeId: 'user' }),
}),
lastModifiedAt: expect.any(String),
lastModifiedBy: expect.objectContaining({
customerRef: expect.objectContaining({ typeId: 'customer' }),
userRef: expect.objectContaining({ typeId: 'user' }),
}),
})
)
);
});
12 changes: 12 additions & 0 deletions models/state/src/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Builder } from '@commercetools-test-data/core';
import generator from './generator';
import transformers from './transformers';
import type { TCreateStateBuilder, TState } from './types';

const Model: TCreateStateBuilder = () =>
Builder<TState>({
generator,
transformers,
});

export default Model;
17 changes: 17 additions & 0 deletions models/state/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const roles = {
ReviewIncludedInStatistics: 'ReviewIncludedInStatistics',
Return: 'Return',
} as const;

const type = {
OrderState: 'OrderState',
LineItemState: 'LineItemState',
ProductState: 'ProductState',
ReviewState: 'ReviewState',
PaymentState: 'PaymentState',
QuoteRequestState: 'QuoteRequestState',
StagedQuoteState: 'StagedQuoteState',
QuoteState: 'QuoteState',
} as const;

export { roles, type };
38 changes: 38 additions & 0 deletions models/state/src/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
ClientLogging,
LocalizedString,
} from '@commercetools-test-data/commons';
import {
sequence,
fake,
Generator,
oneOf,
} from '@commercetools-test-data/core';
import { createRelatedDates } from '@commercetools-test-data/utils';
import { roles, type } from './constants';
import { TState } from './types';

const [getOlderDate, getNewerDate] = createRelatedDates();

// https://docs.commercetools.com/api/projects/States#States

const generator = Generator<TState>({
fields: {
id: fake((f) => f.string.uuid()),
version: sequence(),
key: fake((f) => f.lorem.slug(2)),
type: oneOf(...Object.values(type)),
name: fake(() => LocalizedString.random()),
description: fake(() => LocalizedString.random()),
initial: fake((f) => f.datatype.boolean()),
builtIn: fake((f) => f.datatype.boolean()),
roles: [oneOf(...Object.values(roles))],
transitions: null,
createdAt: fake(getOlderDate),
createdBy: fake(() => ClientLogging.random()),
lastModifiedAt: fake(getNewerDate),
lastModifiedBy: fake(() => ClientLogging.random()),
},
});

export default generator;
7 changes: 7 additions & 0 deletions models/state/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * as StateDraft from './state-draft';
export * as State from '.';

export { default as random } from './builder';
export { default as presets } from './presets';
export * from './types';
export * as constants from './constants';
3 changes: 3 additions & 0 deletions models/state/src/presets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const presets = {};

export default presets;
98 changes: 98 additions & 0 deletions models/state/src/state-draft/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */

import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import type { TStateDraft, TStateDraftGraphql } from '../types';
import * as StateDraft from './index';
import { roles } from '../constants';

describe('builder', () => {
it(
...createBuilderSpec<TStateDraft, TStateDraft>(
'default',
StateDraft.random(),
expect.objectContaining({
key: expect.any(String),
type: expect.any(String),
name: expect.objectContaining({
de: expect.any(String),
en: expect.any(String),
fr: expect.any(String),
}),
description: expect.objectContaining({
en: expect.any(String),
}),
initial: expect.any(Boolean),
roles: expect.any(Array),
transitions: null,
})
)
);

it(
...createBuilderSpec<TStateDraft, TStateDraft>(
'rest',
StateDraft.random(),
expect.objectContaining({
key: expect.any(String),
type: expect.any(String),
name: expect.objectContaining({
de: expect.any(String),
en: expect.any(String),
fr: expect.any(String),
}),
description: expect.objectContaining({
en: expect.any(String),
}),
initial: expect.any(Boolean),
roles: expect.any(Array),
transitions: null,
})
)
);
// Note that the StateDraft graphql is provided as scaffolding only and may not be complete at this time.
it(
...createBuilderSpec<TStateDraft, TStateDraftGraphql>(
'graphql',
StateDraft.random(),
expect.objectContaining({
key: expect.any(String),
type: expect.any(String),
name: expect.arrayContaining([
expect.objectContaining({
locale: 'en',
value: expect.any(String),
}),
expect.objectContaining({
locale: 'de',
value: expect.any(String),
}),
expect.objectContaining({
locale: 'fr',
value: expect.any(String),
}),
]),
description: expect.arrayContaining([
expect.objectContaining({
locale: 'en',
value: expect.any(String),
__typename: 'LocalizedString',
}),
expect.objectContaining({
locale: 'de',
value: expect.any(String),
__typename: 'LocalizedString',
}),
expect.objectContaining({
locale: 'fr',
value: expect.any(String),
__typename: 'LocalizedString',
}),
]),
initial: expect.any(Boolean),
roles: expect.any(Array),
transitions: null,
})
)
);
});
Loading
Loading