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

feat: pojo faker factory #475

Merged
merged 4 commits into from
Oct 28, 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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cats-cradle/create-artifact",
"comment": "",
"type": "none"
}
],
"packageName": "@cats-cradle/create-artifact"
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions libraries/faker-factory/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ module.exports = {
parserOptions: {
tsconfigRootDir: __dirname,
},
rules: {
'no-prototype-builtins': 'off',
},
};
12 changes: 12 additions & 0 deletions libraries/faker-factory/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
{
"name": "@cats-cradle/faker-factory",
"entries": [
{
"version": "1.1.0",
"tag": "@cats-cradle/faker-factory_v1.1.0",
"date": "Sat, 28 Oct 2023 21:42:52 GMT",
"comments": {
"minor": [
{
"comment": "add to pojo option"
}
]
}
},
{
"version": "1.0.4",
"tag": "@cats-cradle/faker-factory_v1.0.4",
Expand Down
9 changes: 8 additions & 1 deletion libraries/faker-factory/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log - @cats-cradle/faker-factory

This log was last generated on Wed, 04 Oct 2023 03:44:49 GMT and should not be manually modified.
This log was last generated on Sat, 28 Oct 2023 21:42:52 GMT and should not be manually modified.

## 1.1.0
Sat, 28 Oct 2023 21:42:52 GMT

### Minor changes

- add to pojo option

## 1.0.4
Wed, 04 Oct 2023 03:44:49 GMT
Expand Down
2 changes: 1 addition & 1 deletion libraries/faker-factory/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cats-cradle/faker-factory",
"version": "1.0.4",
"version": "1.1.0",
"main": "./dist/index.js",
"repository": {
"type": "git",
Expand Down
11 changes: 10 additions & 1 deletion libraries/faker-factory/src/__tests__/faker-factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ describe('FakerFactory', () => {
describe('create', () => {
let faker: SampleClass;

beforeAll(async () => {
beforeEach(async () => {
faker = await FakerFactory.create<SampleClass>(SampleClass);
});

it('should create a pojo faker object', async () => {
faker = await FakerFactory.create<SampleClass>(
SampleClass,
{},
{ pojo: true },
);
expect(typeof faker).toBe('object');
});

it('should create a faker object', () => {
expect(typeof faker).toBe('object');
});
Expand Down
35 changes: 35 additions & 0 deletions libraries/faker-factory/src/__tests__/pojo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { isPojo, toPojo } from '../pojo';

class TestClass {
a: string;

b() {
return this.a;
}
}

describe('toPojo', () => {
it('should convert complex object into plain old javascript object', () => {
const sut = new TestClass();
sut.a = 'Test';

expect(toPojo(sut)).toEqual({ a: 'Test' });
});
});

describe('isPojo', () => {
it('should return false for non-pojo', () => {
const sut = new TestClass();
sut.a = 'Test';

expect(isPojo(sut)).toBeFalsy();
});

it('should return true for pojo', () => {
const sut = new TestClass();
sut.a = 'Test';
const pojo = sut;

expect(isPojo(pojo)).toBeFalsy();
});
});
5 changes: 5 additions & 0 deletions libraries/faker-factory/src/faker-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { cloneDeep } from 'lodash';
import { Settings } from './settings';
import { generateFakeData } from './generate-fake-data';
import { getSchemas } from './schemas';
import { toPojo } from './pojo';

export class FakerFactory {
public static create<T>(
Expand Down Expand Up @@ -30,6 +31,10 @@ export class FakerFactory {
...partialClone,
});

if (settings?.pojo === true) {
return toPojo(objectInstance);
}

return objectInstance as T;
}
}
2 changes: 2 additions & 0 deletions libraries/faker-factory/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './faker-factory';
export { toPojo, isPojo } from './pojo';
export { Settings } from './settings';
34 changes: 34 additions & 0 deletions libraries/faker-factory/src/pojo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* toPojo
* @param obj
* @returns
*/
export function toPojo(obj: any): any {
if (obj === null || typeof obj !== 'object') {
return obj;
}

if (Array.isArray(obj)) {
return obj.map((item) => toPojo(item));
}

const result: { [key: string]: any } = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = toPojo(obj[key]);
}
}

return result;
}

export function isPojo(obj: any): boolean {
if (typeof obj !== 'object' || obj === null) {
return false;
}

const prototype = Object.getPrototypeOf(obj);

// If the prototype is null, it's a plain object
return prototype === null;
}
6 changes: 6 additions & 0 deletions libraries/faker-factory/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export interface Settings {
*/
optionals?: boolean | undefined;

/**
* whether to return a plain old javascript object
* which is helpful when comparing rest request
*/
pojo?: boolean | undefined;

/**
* whether optional properties are shown
*/
Expand Down
12 changes: 12 additions & 0 deletions libraries/nestjs-modules/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
{
"name": "@cats-cradle/nestjs-modules",
"entries": [
{
"version": "0.2.5",
"tag": "@cats-cradle/nestjs-modules_v0.2.5",
"date": "Sat, 28 Oct 2023 21:42:52 GMT",
"comments": {
"dependency": [
{
"comment": "Updating dependency \"@cats-cradle/faker-factory\" to `1.1.0`"
}
]
}
},
{
"version": "0.2.4",
"tag": "@cats-cradle/nestjs-modules_v0.2.4",
Expand Down
7 changes: 6 additions & 1 deletion libraries/nestjs-modules/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log - @cats-cradle/nestjs-modules

This log was last generated on Wed, 04 Oct 2023 03:44:49 GMT and should not be manually modified.
This log was last generated on Sat, 28 Oct 2023 21:42:52 GMT and should not be manually modified.

## 0.2.5
Sat, 28 Oct 2023 21:42:52 GMT

_Version update only_

## 0.2.4
Wed, 04 Oct 2023 03:44:49 GMT
Expand Down
2 changes: 1 addition & 1 deletion libraries/nestjs-modules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cats-cradle/nestjs-modules",
"version": "0.2.4",
"version": "0.2.5",
"main": "./dist/index.js",
"repository": {
"type": "git",
Expand Down
12 changes: 12 additions & 0 deletions libraries/validation-schemas/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
{
"name": "@cats-cradle/validation-schemas",
"entries": [
{
"version": "0.3.1",
"tag": "@cats-cradle/validation-schemas_v0.3.1",
"date": "Sat, 28 Oct 2023 21:42:52 GMT",
"comments": {
"dependency": [
{
"comment": "Updating dependency \"@cats-cradle/faker-factory\" to `1.1.0`"
}
]
}
},
{
"version": "0.3.0",
"tag": "@cats-cradle/validation-schemas_v0.3.0",
Expand Down
7 changes: 6 additions & 1 deletion libraries/validation-schemas/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log - @cats-cradle/validation-schemas

This log was last generated on Sun, 22 Oct 2023 15:17:07 GMT and should not be manually modified.
This log was last generated on Sat, 28 Oct 2023 21:42:52 GMT and should not be manually modified.

## 0.3.1
Sat, 28 Oct 2023 21:42:52 GMT

_Version update only_

## 0.3.0
Sun, 22 Oct 2023 15:17:07 GMT
Expand Down
2 changes: 1 addition & 1 deletion libraries/validation-schemas/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cats-cradle/validation-schemas",
"version": "0.3.0",
"version": "0.3.1",
"main": "./dist/index.js",
"license": "MIT",
"repository": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cats-cradle/create-artifact",
"version": "1.1.1",
"version": "2.0.0",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions services/authentication-service/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
{
"name": "@cats-cradle/authentication-service",
"entries": [
{
"version": "1.0.6",
"tag": "@cats-cradle/authentication-service_v1.0.6",
"date": "Sat, 28 Oct 2023 21:42:52 GMT",
"comments": {
"dependency": [
{
"comment": "Updating dependency \"@cats-cradle/nestjs-modules\" to `0.2.5`"
},
{
"comment": "Updating dependency \"@cats-cradle/faker-factory\" to `1.1.0`"
}
]
}
},
{
"version": "1.0.5",
"tag": "@cats-cradle/authentication-service_v1.0.5",
Expand Down
7 changes: 6 additions & 1 deletion services/authentication-service/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log - @cats-cradle/authentication-service

This log was last generated on Sat, 28 Oct 2023 04:52:47 GMT and should not be manually modified.
This log was last generated on Sat, 28 Oct 2023 21:42:52 GMT and should not be manually modified.

## 1.0.6
Sat, 28 Oct 2023 21:42:52 GMT

_Version update only_

## 1.0.5
Sat, 28 Oct 2023 04:52:47 GMT
Expand Down
2 changes: 1 addition & 1 deletion services/authentication-service/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cats-cradle/authentication-service",
"module": "commonjs",
"version": "1.0.5",
"version": "1.0.6",
"scripts": {
"dev": "nest start --debug --watch",
"build": "tsc",
Expand Down
Loading
Loading