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

fix: write integration test for create shop #6318

Merged
merged 2 commits into from
Feb 2, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mutation CreateShop($input: CreateShopInput!) {
createShop(input: $input) {
shop {
name
description
shopType
timezone
language
currency {
code
}
}
}
}
157 changes: 157 additions & 0 deletions tests/integration/api/mutations/createShop/createShop.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import importAsString from "@reactioncommerce/api-utils/importAsString.js";
import { importPluginsJSONFile, ReactionTestAPICore } from "@reactioncommerce/api-core";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import order needs to be changed.

import Factory from "/tests/util/factory.js";

const CreateShopMutation = importAsString("./CreateShopMutation.graphql");

jest.setTimeout(300000);

let testApp;
let createShop;
const shopId = null;// shop ID is null for account manager and system manager groups. Only these groups can create shop
let mockAdminAccount;
beforeAll(async () => {
testApp = new ReactionTestAPICore();
const plugins = await importPluginsJSONFile("../../../../../plugins.json", (pluginList) => {
// Remove the `files` plugin when testing. Avoids lots of errors.
delete pluginList.files;

return pluginList;
});
await testApp.reactionNodeApp.registerPlugins(plugins);
await testApp.start();

const adminGroup = Factory.Group.makeOne({
_id: "adminGroup",
createdBy: null,
name: "admin",
permissions: ["reaction:legacy:shops/create"],
slug: "admin",
shopId
});
await testApp.collections.Groups.insertOne(adminGroup);

mockAdminAccount = Factory.Account.makeOne({
groups: [adminGroup._id],
shopId: "123"
});
await testApp.createUserAndAccount(mockAdminAccount);

createShop = testApp.mutate(CreateShopMutation);

await testApp.setLoggedInUser(mockAdminAccount);
});

// There is no need to delete any test data from collections because
// testApp.stop() will drop the entire test database. Each integration
// test file gets its own test database.
afterAll(() => testApp.stop());

test("user with `reaction:legacy:shops/create` permission can create shop with name", async () => {
const mockShopSettings = {
name: "My Awesome Shop",
description: "A very awesome shop"
};

let result;
try {
result = await createShop({
input: {
...mockShopSettings
}
});
} catch (error) {
expect(error).toBeUndefined();
return;
}
const expectedShopSettings = {
name: "My Awesome Shop",
shopType: "primary",
description: "A very awesome shop",
timezone: "US/Pacific",
language: "en",
currency: {
code: "USD"
}
};
expect(result.createShop.shop).toEqual(expectedShopSettings);
});

test("user with `reaction:legacy:shops/create` permission cannot create 2nd primary shop type", async () => {
const mockShopSettings = {
name: "Primary shop",
type: "primary"
};

try {
await createShop({
input: {
...mockShopSettings
}
});
} catch (error) {
expect(error).toBeDefined();
expect(error[0].message).toBe("There may be only one primary shop");
return;
}
});

test("user with `reaction:legacy:shops/create` permission can create multiple merchant shop", async () => {
let result;
const mockShopSettings1 = {
name: "Merchant shop 1",
type: "merchant"
};
const mockShopSettings2 = {
name: "Merchant shop 2",
type: "merchant",
defaultTimezone: "US/Eastern",
defaultLanguage: "es"
};

// create 1st merchant shop
try {
result = await createShop({
input: {
...mockShopSettings1
}
});
} catch (error) {
expect(error).toBeUndefined();
return;
}
const expectedShopSettings1 = {
name: "Merchant shop 1",
description: null,
shopType: "merchant",
timezone: "US/Pacific",
language: "en",
currency: {
code: "USD"
}
};
expect(result.createShop.shop).toEqual(expectedShopSettings1);

// create 2nd merchant shop
try {
result = await createShop({
input: {
...mockShopSettings2
}
});
} catch (error) {
expect(error).toBeUndefined();
return;
}
const expectedShopSettings2 = {
name: "Merchant shop 2",
description: null,
shopType: "merchant",
timezone: "US/Eastern",
language: "es",
currency: {
code: "USD"
}
};
expect(result.createShop.shop).toEqual(expectedShopSettings2);
});