Skip to content

Commit

Permalink
feat: create and pass internal config object
Browse files Browse the repository at this point in the history
Create a config object which contains the `storage` and a new boolean
`strict` to make the mock service more strict in the near future
  • Loading branch information
mvantellingen committed Jan 18, 2025
1 parent aa0f9c2 commit 55b0bfc
Show file tree
Hide file tree
Showing 48 changed files with 221 additions and 186 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-ears-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

Add a `strict` boolean option to make the mock service strict and throw errors when certain values are missing
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { AbstractStorage } from "./storage";

export type Config = {
strict: boolean;
storage: AbstractStorage;
};
8 changes: 7 additions & 1 deletion src/ctMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { AbstractStorage } from "./storage";
import { InMemoryStorage } from "./storage";

// Services
import type { Config } from "./config";
import { mapHeaderType } from "./helpers";
import type { RepositoryMap } from "./repositories";
import { createRepositories } from "./repositories";
Expand All @@ -39,6 +40,7 @@ const DEFAULT_OPTIONS: CommercetoolsMockOptions = {
apiHost: DEFAULT_API_HOSTNAME,
authHost: DEFAULT_AUTH_HOSTNAME,
silent: false,
strict: false,
};

const _globalListeners: SetupServer[] = [];
Expand Down Expand Up @@ -122,7 +124,11 @@ export class CommercetoolsMock {
}

private createApp(options?: AppOptions): express.Express {
this._repositories = createRepositories(this._storage);
const config: Config = {
strict: this.options.strict,
storage: this._storage,
};
this._repositories = createRepositories(config);
this._oauth2.setCustomerRepository(this._repositories.customer);

const app = express();
Expand Down
4 changes: 3 additions & 1 deletion src/oauth/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from "express";
import supertest from "supertest";
import { beforeEach, describe, expect, it } from "vitest";
import type { Config } from "~src/config";
import { getBaseResourceProperties } from "../helpers";
import { hashPassword } from "../lib/password";
import { CustomerRepository } from "../repositories/customer";
Expand All @@ -21,7 +22,8 @@ describe("OAuth2Server", () => {
app.use(server.createRouter());

storage = new InMemoryStorage();
customerRepository = new CustomerRepository(storage);
const config: Config = { storage, strict: false };
customerRepository = new CustomerRepository(config);
server.setCustomerRepository(customerRepository);
});

Expand Down
5 changes: 3 additions & 2 deletions src/product-projection-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
RangeFacetResult,
TermFacetResult,
} from "@commercetools/platform-sdk";
import type { Config } from "./config";
import { CommercetoolsError } from "./exceptions";
import { nestedLookup } from "./helpers";
import type {
Expand Down Expand Up @@ -51,8 +52,8 @@ export type ProductProjectionSearchParams = {
export class ProductProjectionSearch {
protected _storage: AbstractStorage;

constructor(storage: AbstractStorage) {
this._storage = storage;
constructor(config: Config) {
this._storage = config.storage;
}

search(
Expand Down
5 changes: 3 additions & 2 deletions src/product-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ProductSearchRequest,
ProductSearchResult,
} from "@commercetools/platform-sdk";
import type { Config } from "./config";
import { CommercetoolsError } from "./exceptions";
import { parseSearchQuery } from "./lib/productSearchFilter";
import { validateSearchQuery } from "./lib/searchQueryTypeChecker";
Expand All @@ -15,8 +16,8 @@ import type { AbstractStorage } from "./storage";
export class ProductSearch {
protected _storage: AbstractStorage;

constructor(storage: AbstractStorage) {
this._storage = storage;
constructor(config: Config) {
this._storage = config.storage;
}

search(
Expand Down
12 changes: 8 additions & 4 deletions src/repositories/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
UpdateAction,
} from "@commercetools/platform-sdk";
import deepEqual from "deep-equal";
import type { Config } from "~src/config";
import { CommercetoolsError } from "~src/exceptions";
import { cloneObject } from "../helpers";
import type { AbstractStorage } from "../storage";
Expand Down Expand Up @@ -40,10 +41,13 @@ export type RepositoryContext = {
export abstract class AbstractRepository<R extends BaseResource | Project> {
protected _storage: AbstractStorage;

protected config: Config;

protected actions: AbstractUpdateHandler | undefined;

constructor(storage: AbstractStorage) {
this._storage = storage;
constructor(config: Config) {
this.config = config;
this._storage = config.storage;
}

abstract saveNew({ projectKey }: RepositoryContext, resource: R): void;
Expand Down Expand Up @@ -92,8 +96,8 @@ export abstract class AbstractResourceRepository<
> extends AbstractRepository<ResourceMap[T]> {
protected _typeId: T;

constructor(typeId: T, storage: AbstractStorage) {
super(storage);
constructor(typeId: T, config: Config) {
super(config);
this._typeId = typeId;
}

Expand Down
6 changes: 3 additions & 3 deletions src/repositories/associate-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type {
AssociateRoleSetPermissionsAction,
AssociateRoleUpdateAction,
} from "@commercetools/platform-sdk";
import type { Config } from "~src/config";
import { getBaseResourceProperties } from "../helpers";
import type { AbstractStorage } from "../storage/abstract";
import type { Writable } from "../types";
import type { UpdateHandlerInterface } from "./abstract";
import {
Expand All @@ -22,8 +22,8 @@ import {
import { createCustomFields } from "./helpers";

export class AssociateRoleRepository extends AbstractResourceRepository<"associate-role"> {
constructor(storage: AbstractStorage) {
super("associate-role", storage);
constructor(config: Config) {
super("associate-role", config);
this.actions = new AssociateRoleUpdateHandler(this._storage);
}

Expand Down
6 changes: 3 additions & 3 deletions src/repositories/attribute-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type {
AttributeGroupSetKeyAction,
AttributeGroupUpdateAction,
} from "@commercetools/platform-sdk";
import type { Config } from "~src/config";
import { getBaseResourceProperties } from "../helpers";
import type { AbstractStorage } from "../storage/abstract";
import type { Writable } from "../types";
import type { UpdateHandlerInterface } from "./abstract";
import {
Expand All @@ -18,8 +18,8 @@ import {
} from "./abstract";

export class AttributeGroupRepository extends AbstractResourceRepository<"attribute-group"> {
constructor(storage: AbstractStorage) {
super("attribute-group", storage);
constructor(config: Config) {
super("attribute-group", config);
this.actions = new AttributeGroupUpdateHandler(this._storage);
}

Expand Down
6 changes: 3 additions & 3 deletions src/repositories/business-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
type Company,
type Division,
} from "@commercetools/platform-sdk";
import type { AbstractStorage } from "~src/storage";
import type { Config } from "~src/config";
import { generateRandomString, getBaseResourceProperties } from "../helpers";
import type { Writable } from "../types";
import type { UpdateHandlerInterface } from "./abstract";
Expand All @@ -41,8 +41,8 @@ import {
} from "./helpers";

export class BusinessUnitRepository extends AbstractResourceRepository<"business-unit"> {
constructor(storage: AbstractStorage) {
super("business-unit", storage);
constructor(config: Config) {
super("business-unit", config);
this.actions = new BusinessUnitUpdateHandler(this._storage);
}

Expand Down
10 changes: 4 additions & 6 deletions src/repositories/cart-discount/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type {
CartDiscountValueGiftLineItem,
CartDiscountValueRelative,
} from "@commercetools/platform-sdk";
import type { Config } from "~src/config";
import { getBaseResourceProperties } from "~src/helpers";
import type { AbstractStorage } from "~src/storage/abstract";
import {
AbstractResourceRepository,
type RepositoryContext,
Expand All @@ -21,9 +21,9 @@ import {
import { CartDiscountUpdateHandler } from "./actions";

export class CartDiscountRepository extends AbstractResourceRepository<"cart-discount"> {
constructor(storage: AbstractStorage) {
super("cart-discount", storage);
this.actions = new CartDiscountUpdateHandler(storage);
constructor(config: Config) {
super("cart-discount", config);
this.actions = new CartDiscountUpdateHandler(config.storage);
}

create(context: RepositoryContext, draft: CartDiscountDraft): CartDiscount {
Expand Down Expand Up @@ -80,7 +80,5 @@ export class CartDiscountRepository extends AbstractResourceRepository<"cart-dis
} as CartDiscountValueRelative;
}
}

return value;
}
}
4 changes: 3 additions & 1 deletion src/repositories/cart/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { CartDraft, LineItem } from "@commercetools/platform-sdk";
import { describe, expect, test } from "vitest";
import type { Config } from "~src/config";
import { InMemoryStorage } from "~src/storage";
import { CartRepository } from "./index";

describe("Cart repository", () => {
const storage = new InMemoryStorage();
const repository = new CartRepository(storage);
const config: Config = { storage, strict: false };
const repository = new CartRepository(config);

test("create cart in store", async () => {
storage.add("dummy", "product", {
Expand Down
6 changes: 3 additions & 3 deletions src/repositories/cart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
type ProductPagedQueryResponse,
} from "@commercetools/platform-sdk";
import { v4 as uuidv4 } from "uuid";
import type { Config } from "~src/config";
import { CommercetoolsError } from "~src/exceptions";
import { getBaseResourceProperties } from "~src/helpers";
import type { AbstractStorage } from "~src/storage/abstract";
import type { Writable } from "~src/types";
import {
AbstractResourceRepository,
Expand All @@ -21,8 +21,8 @@ import { CartUpdateHandler } from "./actions";
import { calculateCartTotalPrice, selectPrice } from "./helpers";

export class CartRepository extends AbstractResourceRepository<"cart"> {
constructor(storage: AbstractStorage) {
super("cart", storage);
constructor(config: Config) {
super("cart", config);
this.actions = new CartUpdateHandler(this._storage);
}

Expand Down
4 changes: 3 additions & 1 deletion src/repositories/category/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { describe, expect, test } from "vitest";
import type { Config } from "~src/config";
import { InMemoryStorage } from "~src/storage";
import { CategoryRepository } from "./index";

describe("Order repository", () => {
const storage = new InMemoryStorage();
const repository = new CategoryRepository(storage);
const config: Config = { storage, strict: false };
const repository = new CategoryRepository(config);

test("valid ancestors", async () => {
const root = repository.create(
Expand Down
6 changes: 3 additions & 3 deletions src/repositories/category/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type {
CategoryReference,
} from "@commercetools/platform-sdk";
import { v4 as uuidv4 } from "uuid";
import type { Config } from "~src/config";
import { getBaseResourceProperties } from "~src/helpers";
import { parseExpandClause } from "~src/lib/expandParser";
import type { AbstractStorage } from "~src/storage/abstract";
import type { Writable } from "~src/types";
import type { GetParams } from "../abstract";
import {
Expand All @@ -17,8 +17,8 @@ import { createCustomFields } from "../helpers";
import { CategoryUpdateHandler } from "./actions";

export class CategoryRepository extends AbstractResourceRepository<"category"> {
constructor(storage: AbstractStorage) {
super("category", storage);
constructor(config: Config) {
super("category", config);
this.actions = new CategoryUpdateHandler(this._storage);
}

Expand Down
6 changes: 3 additions & 3 deletions src/repositories/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type {
ChannelSetGeoLocationAction,
ChannelUpdateAction,
} from "@commercetools/platform-sdk";
import type { Config } from "~src/config";
import { getBaseResourceProperties } from "../helpers";
import type { AbstractStorage } from "../storage/abstract";
import type { Writable } from "../types";
import type { UpdateHandlerInterface } from "./abstract";
import {
Expand All @@ -22,8 +22,8 @@ import {
import { createAddress, createCustomFields } from "./helpers";

export class ChannelRepository extends AbstractResourceRepository<"channel"> {
constructor(storage: AbstractStorage) {
super("channel", storage);
constructor(config: Config) {
super("channel", config);
this.actions = new ChannelUpdateHandler(this._storage);
}

Expand Down
6 changes: 3 additions & 3 deletions src/repositories/custom-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import type {
CustomObjectDraft,
InvalidOperationError,
} from "@commercetools/platform-sdk";
import type { Config } from "~src/config";
import { CommercetoolsError } from "~src/exceptions";
import type { AbstractStorage } from "~src/storage";
import { cloneObject, getBaseResourceProperties } from "../helpers";
import type { Writable } from "../types";
import type { QueryParams } from "./abstract";
import { AbstractResourceRepository, type RepositoryContext } from "./abstract";
import { checkConcurrentModification } from "./errors";

export class CustomObjectRepository extends AbstractResourceRepository<"key-value-document"> {
constructor(storage: AbstractStorage) {
super("key-value-document", storage);
constructor(config: Config) {
super("key-value-document", config);
}

create(
Expand Down
8 changes: 4 additions & 4 deletions src/repositories/customer-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type {
CustomerGroupSetKeyAction,
CustomerGroupUpdateAction,
} from "@commercetools/platform-sdk";
import type { Config } from "~src/config";
import { getBaseResourceProperties } from "../helpers";
import type { AbstractStorage } from "../storage/abstract";
import type { Writable } from "../types";
import type { UpdateHandlerInterface } from "./abstract";
import {
Expand All @@ -19,9 +19,9 @@ import {
import { createCustomFields } from "./helpers";

export class CustomerGroupRepository extends AbstractResourceRepository<"customer-group"> {
constructor(storage: AbstractStorage) {
super("customer-group", storage);
this.actions = new CustomerGroupUpdateHandler(storage);
constructor(config: Config) {
super("customer-group", config);
this.actions = new CustomerGroupUpdateHandler(config.storage);
}

create(context: RepositoryContext, draft: CustomerGroupDraft): CustomerGroup {
Expand Down
4 changes: 3 additions & 1 deletion src/repositories/customer/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Store } from "@commercetools/platform-sdk";
import { describe, expect, test } from "vitest";
import type { Config } from "~src/config";
import { InMemoryStorage } from "~src/storage";
import { CustomerRepository } from "./index";

describe("Customer repository", () => {
const storage = new InMemoryStorage();
const repository = new CustomerRepository(storage);
const config: Config = { storage, strict: false };
const repository = new CustomerRepository(config);

test("query by lowercaseEmail", async () => {
const customer = repository.create(
Expand Down
Loading

0 comments on commit 55b0bfc

Please sign in to comment.