Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
fix(customer): change logic, tests and other parts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Kucmus committed Oct 22, 2019
1 parent 0e9d73e commit 9ba6de4
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 429 deletions.
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
},
"devDependencies": {
"@microsoft/api-extractor": "^7.3.9",
"@types/faker": "^4.1.6",
"@types/jest": "^24.0.18",
"axios": "^0.19.0",
"brotli": "^1.3.2",
"chalk": "^2.4.2",
"coveralls": "^3.0.6",
"execa": "^3.0.0",
"faker": "^4.1.0",
"fs-extra": "^8.1.0",
"jest": "^24.9.0",
"lerna": "^3.16.4",
Expand All @@ -50,11 +52,7 @@
"ts-jest": "^24.0.2",
"typedoc": "^0.15.0",
"typescript": "^3.5.3",
"yorkie": "^2.0.0",
"faker": "^4.1.0",
"jest-when": "^2.7.0",
"@types/faker": "^4.1.6",
"@types/jest-when": "^2.7.0"
"yorkie": "^2.0.0"
},
"peerDependencies": {
"axios": "^0.19.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { when } from "jest-when";
import { address, name, random } from "faker";
import { createCustomerAddress } from "../../../src/index";
import {
createCustomerAddress,
update,
config
} from "@shopware-pwa/shopware-6-client";
import { getCustomerAddressEndpoint } from "../../../src/endpoints";
import { apiService } from "../../../src/apiService";

Expand All @@ -17,23 +20,26 @@ describe("CustomerService - createCustomerAddress", () => {
city: address.city(),
street: address.streetName()
};
let contextToken: string;

beforeEach(() => {
jest.resetAllMocks();
contextToken = random.uuid();
update({ contextToken });
});
afterEach(() => {
expect(config.contextToken).toEqual(contextToken);
});
it("should return created address id", async () => {
when(mockedAxios.post)
.expectCalledWith(getCustomerAddressEndpoint(), newAddressData)
.mockReturnValueOnce({ data: "2bbb89dfa4664bc581e80b37eaa80fb7" });
mockedAxios.post.mockResolvedValueOnce({
data: "2bbb89dfa4664bc581e80b37eaa80fb7"
});
const result = await createCustomerAddress(newAddressData);
expect(result).toEqual("2bbb89dfa4664bc581e80b37eaa80fb7");
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith(
getCustomerAddressEndpoint(),
newAddressData,
{
headers: { "sw-context-token": undefined }
}
newAddressData
);
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { getCustomerAddressEndpoint } from "../../../src/endpoints";
import { apiService } from "../../../src/apiService";
import { deleteCustomerAddress } from "../../../src";
import {
deleteCustomerAddress,
update,
config
} from "@shopware-pwa/shopware-6-client";
import { random } from "faker";

jest.mock("../../../src/apiService");
const mockedAxios = apiService as jest.Mocked<typeof apiService>;

describe("CustomerService - deleteCustomerAddress", () => {
let contextToken: string;

beforeEach(() => {
jest.resetAllMocks();
contextToken = random.uuid();
update({ contextToken });
});
afterEach(() => {
expect(config.contextToken).toEqual(contextToken);
});

it("rejects the promise if the address does not exist", async () => {
Expand All @@ -19,20 +31,17 @@ describe("CustomerService - deleteCustomerAddress", () => {
).rejects.toThrow("400 - customer's address not found");
expect(mockedAxios.delete).toBeCalledTimes(1);
expect(mockedAxios.delete).toBeCalledWith(
getCustomerAddressEndpoint("2bbb89dfa4664bc681e80b37eaa80fb7"),
{ headers: { "sw-context-token": undefined } }
getCustomerAddressEndpoint("2bbb89dfa4664bc681e80b37eaa80fb7")
);
});

it("returns no data if successfully deleted", async () => {
mockedAxios.delete.mockResolvedValue(null);
expect(
await deleteCustomerAddress("2bbb89dfa4664bc681e80b37eaa80fb7")
).toBeNull();

await deleteCustomerAddress("2bbb89dfa4664bc681e80b37eaa80fb7");
expect(mockedAxios.delete).toBeCalledTimes(1);
expect(mockedAxios.delete).toBeCalledWith(
getCustomerAddressEndpoint("2bbb89dfa4664bc681e80b37eaa80fb7"),
{ headers: { "sw-context-token": undefined } }
getCustomerAddressEndpoint("2bbb89dfa4664bc681e80b37eaa80fb7")
);
});
});
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
import { when } from "jest-when";
import { getCustomer } from "@shopware-pwa/shopware-6-client";
import { getCustomer, update, config } from "@shopware-pwa/shopware-6-client";
import { apiService } from "../../../src/apiService";
import { getCustomerEndpoint } from "../../../src/endpoints";
import { random } from "faker";
jest.mock("../../../src/apiService");
const mockedAxios = apiService as jest.Mocked<typeof apiService>;

describe("CustomerService - getCustomer", () => {
let contextToken: string;
beforeEach(() => {
jest.resetAllMocks();
contextToken = random.uuid();
update({ contextToken });
});
afterEach(() => {
afterEach(() => {
expect(config.contextToken).toEqual(contextToken);
});
});

it("should an current customer's data - using correct token", async () => {
when(mockedAxios.get)
.expectCalledWith(getCustomerEndpoint(), {
headers: { "sw-context-token": "c370eb5cd1df4d4dbcc78f055b693e79" }
})
.mockReturnValueOnce({
data: { data: { id: "c370eb5cd1df4d4dbcc78f055b693e79" } }
});
const result = await getCustomer("c370eb5cd1df4d4dbcc78f055b693e79");
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith("/customer", {
headers: { "sw-context-token": "c370eb5cd1df4d4dbcc78f055b693e79" }
mockedAxios.get.mockResolvedValueOnce({
data: { data: { id: "c370eb5cd1df4d4dbcc78f055b693e79" } }
});
expect(result).toHaveProperty("id");
const result = await getCustomer();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith(getCustomerEndpoint());
expect(result.id).toEqual("c370eb5cd1df4d4dbcc78f055b693e79");
});

it("should an current customer's data - using incorrect token", async () => {
mockedAxios.get.mockRejectedValue(new Error());
expect(getCustomer("b370eb5cd1df4d4dbcc78f055b693e79")).rejects.toThrow();
expect(getCustomer()).rejects.toThrow();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith("/customer", {
headers: { "sw-context-token": "b370eb5cd1df4d4dbcc78f055b693e79" }
});
expect(mockedAxios.get).toBeCalledWith(getCustomerEndpoint());
});
});
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import { when } from "jest-when";
import { getCustomerAddress } from "../../../src/index";
import {
getCustomerAddress,
update,
config
} from "@shopware-pwa/shopware-6-client";
import { getCustomerAddressEndpoint } from "../../../src/endpoints";
import { apiService } from "../../../src/apiService";
import { random } from "faker";

jest.mock("../../../src/apiService");
const mockedAxios = apiService as jest.Mocked<typeof apiService>;

describe("CustomerService - getCustomerAddress", () => {
let contextToken: string;
beforeEach(() => {
jest.resetAllMocks();

contextToken = random.uuid();

update({ contextToken });
});
afterEach(() => {
// expect(
// mockedAxios.defaults.headers.common["sw-context-token"]
// ).toEqual(contextToken);
expect(config.contextToken).toEqual(contextToken);
});

it("should return address object", async () => {
when(mockedAxios.get)
.expectCalledWith(
getCustomerAddressEndpoint("2bbb89dfa4664bc681e80b37eaa80fb7")
)
.mockReturnValueOnce({
data: { data: { id: "2bbb89dfa4664bc681e80b37eaa80fb7" } }
});
mockedAxios.get.mockResolvedValueOnce({
data: { data: { id: "2bbb89dfa4664bc681e80b37eaa80fb7" } }
});
const result = await getCustomerAddress("2bbb89dfa4664bc681e80b37eaa80fb7");
expect(result.id).toEqual("2bbb89dfa4664bc681e80b37eaa80fb7");
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith(
getCustomerAddressEndpoint("2bbb89dfa4664bc681e80b37eaa80fb7"),
{
headers: { "sw-context-token": undefined }
}
getCustomerAddressEndpoint("2bbb89dfa4664bc681e80b37eaa80fb7")
);
});

Expand All @@ -38,8 +47,7 @@ describe("CustomerService - getCustomerAddress", () => {
);
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith(
getCustomerAddressEndpoint("wrong-id"),
{ headers: { "sw-context-token": undefined } }
getCustomerAddressEndpoint("wrong-id")
);
});
});
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { random } from "faker";
import { when } from "jest-when";
import { getCustomerAddresses } from "../../../src/index";
import {
getCustomerAddresses,
update,
config
} from "@shopware-pwa/shopware-6-client";
import { getCustomerEndpoint } from "../../../src/endpoints";
import { apiService } from "../../../src/apiService";

jest.mock("../../../src/apiService");
const mockedAxios = apiService as jest.Mocked<typeof apiService>;

describe("CustomerService - register", () => {
const customerToken = random.uuid();
let contextToken: string;
beforeEach(() => {
jest.resetAllMocks();
contextToken = random.uuid();
update({ contextToken });
});
afterEach(() => {
afterEach(() => {
expect(config.contextToken).toEqual(contextToken);
});
});

it("should return object of addresses", async () => {
when(mockedAxios.get)
.expectCalledWith(`${getCustomerEndpoint()}/address`)
.mockReturnValueOnce({ data: { data: {} } });
const result = await getCustomerAddresses(customerToken);
mockedAxios.get.mockResolvedValueOnce({ data: { data: {} } });
const result = await getCustomerAddresses();
expect(mockedAxios.get).toBeCalledTimes(1);
expect(mockedAxios.get).toBeCalledWith(`${getCustomerEndpoint()}/address`, {
headers: { "sw-context-token": customerToken }
});
expect(mockedAxios.get).toBeCalledWith(`${getCustomerEndpoint()}/address`);
expect(result).toMatchObject({});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { login } from "../../../src/index";
import { when } from "jest-when";
import { login, config } from "@shopware-pwa/shopware-6-client";
import { getCustomerLoginEndpoint } from "../../../src/endpoints";
import { apiService } from "../../../src/apiService";
import { internet } from "faker";
Expand All @@ -17,14 +16,10 @@ describe("CustomerService - login", () => {
jest.resetAllMocks();
});
it("should return context token", async () => {
when(mockedAxios.post)
.expectCalledWith(getCustomerLoginEndpoint(), {
username: credentials.username,
password: credentials.password
})
.mockReturnValueOnce({
data: { "sw-context-token": "RmzTExFStSBW5GhPmQNicSK6bhUQhqXi" }
});
mockedAxios.post.mockResolvedValueOnce({
data: { "sw-context-token": "RmzTExFStSBW5GhPmQNicSK6bhUQhqXi" }
});

const result = await login({
username: credentials.username,
password: credentials.password
Expand All @@ -34,16 +29,12 @@ describe("CustomerService - login", () => {
username: credentials.username,
password: credentials.password
});
expect(result).toHaveProperty("sw-context-token");

expect(result.contextToken).toEqual("RmzTExFStSBW5GhPmQNicSK6bhUQhqXi");
expect(config.contextToken).toEqual("RmzTExFStSBW5GhPmQNicSK6bhUQhqXi");
});
it("should throws unhandled rejection - 401", async () => {
when(mockedAxios.post.mockRejectedValue(new Error())).expectCalledWith(
getCustomerLoginEndpoint(),
{
username: credentials.username,
password: "wrong-password-123456"
}
);
mockedAxios.post.mockRejectedValue(new Error());

expect(
login({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { logout } from "../../../src/index";
import { when } from "jest-when";
import { logout } from "@shopware-pwa/shopware-6-client";
import { getCustomerLogoutEndpoint } from "../../../src/endpoints";
import { apiService } from "../../../src/apiService";

Expand All @@ -12,14 +11,9 @@ describe("CustomerService - logout", () => {
});

it("should log out the customer", async () => {
when(mockedAxios.post)
.expectCalledWith(getCustomerLogoutEndpoint())
.mockReturnValueOnce("");
const result = await logout();
mockedAxios.get.mockResolvedValueOnce({ data: null });
await logout();
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith(getCustomerLogoutEndpoint(), null, {
headers: { "sw-context-token": undefined }
});
expect(result).toBeUndefined();
expect(mockedAxios.post).toBeCalledWith(getCustomerLogoutEndpoint());
});
});
Loading

0 comments on commit 9ba6de4

Please sign in to comment.