Skip to content

Commit

Permalink
feat(service): Add contextService and dummy tests against real Shopwa…
Browse files Browse the repository at this point in the history
…re API
  • Loading branch information
rmakara committed Oct 15, 2019
1 parent c37de17 commit 48c1edf
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ContextService } from "../../src/index";

describe("ContextService", () => {
describe("getCurrencies", () => {
it("should return all currencies", async () => {
try {
const result = await ContextService.getCurrencies();
expect(result.data).toHaveLength(3);
} catch (e) {
console.error("Connection problem", e);
}
});
});

describe("getLanguages", () => {
it("should return all languages", async () => {
try {
const result = await ContextService.getLanguages();
expect(result.data).toHaveLength(2);
} catch (e) {
console.error("Connection problem", e);
}
});
});

describe("getCountries", () => {
it("should return all countries", async () => {
try {
const result = await ContextService.getLanguages();
expect(result.data).toHaveLength(2);
} catch (e) {
console.error("Connection problem", e);
}
});
});

describe("getPaymentMethods", () => {
it("should return all payment methods", async () => {
try {
const result = await ContextService.getPaymentMethods();
expect(result.data).toHaveLength(4);
} catch (e) {
console.error("Connection problem", e);
}
});
});

describe("getShippingMethods", () => {
it("should return all shipping methods", async () => {
try {
const result = await ContextService.getShippingMethods();
expect(result.data).toHaveLength(2);
} catch (e) {
console.error("Connection problem", e);
}
});
});
});
1 change: 1 addition & 0 deletions packages/shopware-6-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { reloadConfiguration } from "./apiService";
export { config } from "./settings";
export { Category, CategoryService } from "./categoryService";
export { ProductService } from "./services/productService";
export { ContextService } from "./services/contextService";

export function setup(config: ClientSettings = {}): void {
setupConfig(config);
Expand Down
75 changes: 75 additions & 0 deletions packages/shopware-6-client/src/services/contextService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Currency } from "../interfaces/models/system/currency/Currency";
import { apiService } from "../apiService";
import { config } from "../settings";
import {
getContextCurrencyEndpoint,
getContextCountryEndpoint,
getContextPaymentMethodEndpoint,
getContextShippingMethodEndpoint,
getContextLanguageEndpoint
} from "../endpoints";
import { Country } from "../interfaces/models/system/country/Country";
import { ShippingMethod } from "../interfaces/models/checkout/shipping/ShippingMethod";
import { PaymentMethod } from "../interfaces/models/checkout/payment/PaymentMethod";
import { Language } from "../interfaces/models/framework/language/Language";
import { SearchResult } from "../interfaces/response/SearchResult";

const getCurrencies = async function(): Promise<SearchResult<Currency[]>> {
const resp = await apiService.get(
config.endpoint + getContextCurrencyEndpoint()
);

return resp.data;
};

const getLanguages = async function(): Promise<SearchResult<Language[]>> {
const resp = await apiService.get(
config.endpoint + getContextLanguageEndpoint()
);

return resp.data;
};

const getCountries = async function(): Promise<SearchResult<Country[]>> {
const resp = await apiService.get(
config.endpoint + getContextCountryEndpoint()
);

return resp.data;
};

const getPaymentMethods = async function(): Promise<
SearchResult<PaymentMethod[]>
> {
const resp = await apiService.get(
config.endpoint + getContextPaymentMethodEndpoint()
);

return resp.data;
};

const getShippingMethods = async function(): Promise<
SearchResult<ShippingMethod[]>
> {
const resp = await apiService.get(
config.endpoint + getContextShippingMethodEndpoint()
);

return resp.data;
};

interface ContextService {
getCurrencies: () => Promise<SearchResult<Currency[]>>;
getLanguages: () => Promise<SearchResult<Language[]>>;
getCountries: () => Promise<SearchResult<Country[]>>;
getPaymentMethods: () => Promise<SearchResult<PaymentMethod[]>>;
getShippingMethods: () => Promise<SearchResult<ShippingMethod[]>>;
}

export const ContextService: ContextService = {
getCurrencies,
getLanguages,
getCountries,
getPaymentMethods,
getShippingMethods
};

0 comments on commit 48c1edf

Please sign in to comment.