Skip to content

Commit

Permalink
test: Update of semantics
Browse files Browse the repository at this point in the history
- Strapi build types, added more complex query with dynamic zones.
- Fixed test description to be more fluent in reading.
- Added test for _isDefined util
  • Loading branch information
Refrezsh committed Aug 16, 2023
1 parent bd2a434 commit f5b9fa3
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build": "npm run clean && npm run build:esm && npm run build:cjs && npm run pretty",
"prepack": "npm run build",
"test": "npm run build && jest",
"speed": "node tests/performanceTest.js",
"speed": "node tests/performance-test.js",
"coverage": "jest --coverage",
"semantic-release": "semantic-release"
},
Expand Down
67 changes: 50 additions & 17 deletions tests/build-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const strapiService = {
$and: [
{
attribute: {
$eq: "attribute",
$eq: "value",
},
},
{
nested: {
attribute: {
$eq: "attribute",
$eq: "value",
},
},
},
Expand All @@ -22,7 +22,14 @@ const strapiService = {
attribute: {
filters: {
attribute: {
$eq: "attribute",
$eq: "value",
},
},
},
dynamicZone: {
on: {
component: {
fields: ["attribute"],
},
},
},
Expand All @@ -32,6 +39,7 @@ const strapiService = {
locale: "uk",
pagination: {
page: 5,
pageSize: 24,
withCount: true,
},
};
Expand All @@ -41,13 +49,13 @@ const entityService = {
$and: [
{
attribute: {
$eq: "attribute",
$eq: "value",
},
},
{
nested: {
attribute: {
$eq: "attribute",
$eq: "value",
},
},
},
Expand All @@ -58,27 +66,35 @@ const entityService = {
attribute: {
filters: {
attribute: {
$eq: "attribute",
$eq: "value",
},
},
},
dynamicZone: {
on: {
component: {
fields: ["attribute"],
},
},
},
},
fields: ["attribute"],
page: 5,
pageSize: 24,
};

const queryEngine = {
where: {
$and: [
{
attribute: {
$eq: "attribute",
$eq: "value",
},
},
{
nested: {
attribute: {
$eq: "attribute",
$eq: "value",
},
},
},
Expand All @@ -89,30 +105,47 @@ const queryEngine = {
attribute: {
where: {
attribute: {
$eq: "attribute",
$eq: "value",
},
},
},
dynamicZone: {
on: {
component: {
select: ["attribute"],
},
},
},
},
select: ["attribute"],
start: 5,
limit: 24,
};

describe("Strapi query build types", () => {
it("should build strapi proper structure queries", () => {
describe("Strapi query builder", () => {
it("should build proper strapi structure of queries", () => {
const query = new SQBuilder()
.sort("attribute")
.asc()
.fields("attribute")
.filters("attribute", (b) => b.eq("attribute"))
.filters("nested.attribute", (b) => b.eq("attribute"))
.populate("attribute", (b) => b.filters("attribute").eq("attribute"))
.filters("attribute", (b) => b.eq("value"))
.filters("nested.attribute", (b) => b.eq("value"))
.populate("attribute", (b) => b.filters("attribute").eq("value"))
.populate("dynamicZone", (dynamicZoneBuilder) => {
dynamicZoneBuilder.on("component", (b) => b.fields("attribute"));
})
.publicationState("preview")
.locale("uk");

const serviceBuilt = query.page(5, true).buildStrapiService();
const entityServiceBuilt = query.page(5, true).buildEntityService();
const queryEngineBuilt = query.pageStart(5).buildQueryEngine();
const serviceBuilt = query.page(5, true).pageSize(24).buildStrapiService();
const entityServiceBuilt = query
.page(5, true)
.pageSize(24)
.buildEntityService();
const queryEngineBuilt = query
.pageStart(5)
.pageLimit(24)
.buildQueryEngine();

expect(serviceBuilt).toEqual(strapiService);
expect(entityServiceBuilt).toEqual(entityService);
Expand Down
4 changes: 2 additions & 2 deletions tests/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import SQBuilder from "../lib/cjs";

const dataObject = { key: "value", data: ["value2"] };

describe("Data", () => {
it("should be data", () => {
describe("Data operator", () => {
it("should be same as input", () => {
const query = new SQBuilder().data(dataObject).build();
expect(query.data).toEqual(dataObject);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const mainKey2 = "key2";
const keyType1 = "keyType1";
const keyType2 = "ketType2";

describe("Populate morph ", () => {
it("Morph single population", () => {
describe("Dynamic zone operator", () => {
it("should create proper query", () => {
const builtQuery = new SQBuilder()
.populate(mainKey, (keyBuilder) => {
keyBuilder
Expand All @@ -27,7 +27,7 @@ describe("Populate morph ", () => {
expect(builtQuery).toEqual(morphQuery);
});

it("Morph multiple population", () => {
it("should create multiple dynamic zones query", () => {
const builtQuery = new SQBuilder()
.populate(mainKey, (keyBuilder) => {
keyBuilder
Expand Down Expand Up @@ -60,7 +60,7 @@ describe("Populate morph ", () => {
expect(builtQuery).toEqual(twoMorphQuery);
});

it("Morph same key", () => {
it("should select last population for same dynamic zones", () => {
const builtQuery = new SQBuilder()
.populate(mainKey, (keyBuilder) => {
keyBuilder
Expand Down Expand Up @@ -93,7 +93,7 @@ describe("Populate morph ", () => {
expect(builtQuery).toEqual(morphQuery);
});

it("Morph population join", () => {
it("should join", () => {
const query1 = new SQBuilder().populate(mainKey, (keyBuilder) => {
keyBuilder
.on(keyType1, (typeBuilder) => {
Expand Down
12 changes: 6 additions & 6 deletions tests/empty-operators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ const emptyObject = {};
const attribute = "attr";

describe("Empty operators", () => {
it("Logical empty", () => {
it("should be empty for logical filters", () => {
const query = new SQBuilder().or().and().not().build();
expect(query).toEqual(emptyObject);
});

it("Filters empty", () => {
it("should be empty for filters without selected operator", () => {
const query = new SQBuilder().filters(attribute).filters(attribute).build();
expect(query).toEqual(emptyObject);
});

it("Attribute filters empty", () => {
const query = new SQBuilder();
it("should be empty for all operators without filters request", () => {
const query = new SQBuilder<any, any>();

for (const attr of attributeFilters) {
const fnAttr = attr.replace("$", "") as keyof SQBuilder<object>;
// @ts-ignore FIXME Why is it impossible (It's works but ts can't inherit function type from class by key)
const fnAttr = attr.replace("$", "") as keyof SQBuilder<any, any>;
// @ts-ignore TODO: Typescript won't iterate by keyof SQBuilder
query[fnAttr](attribute);
}

Expand Down
10 changes: 5 additions & 5 deletions tests/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ const filed1 = "key2";

const getFields = (key: string, key2: string) => ({ fields: [key, key2] });

describe("Fields query", () => {
it("Chain", () => {
describe("Field operator", () => {
it("should create query by chain", () => {
const builtQuery = new SQBuilder().fields(field).fields(filed1).build();

expect(builtQuery).toEqual(getFields(field, filed1));
});

it("Array", () => {
it("should create query by array", () => {
const builtQuery = new SQBuilder().fields([field, filed1]).build();

expect(builtQuery).toEqual(getFields(field, filed1));
});

it("Same keys", () => {
it("should merge same keys", () => {
const builtQuery = new SQBuilder()
.fields([field, filed1, field, filed1])
.build();

expect(builtQuery).toEqual(getFields(field, filed1));
});

it("Join", () => {
it("should join query and merge same keys", () => {
const query1 = new SQBuilder().fields([field]);
const query2 = new SQBuilder().fields([filed1]);
const query3 = new SQBuilder().fields([field]);
Expand Down
45 changes: 24 additions & 21 deletions tests/filters-complex-level.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import SQBuilder from "../lib/cjs";

describe("Filters - complex cases", () => {
it("Double nested - without root attribute", () => {
describe("Filters operator", () => {
it("should create nested filters without attribute", () => {
const builtQuery = new SQBuilder()
.and()
.filters()
.with((nestedBuilder) =>
nestedBuilder
.or()
.filters("createdAt")
.lte("date1")
.filters("createdAt")
.gte("date2")
.filters((b) =>
b.with((nestedBuilder) =>
nestedBuilder
.or()
.filters("createdAt")
.lte("date1")
.filters("createdAt")
.gte("date2")
)
)

.filters()
.with((nestedBuilder) =>
nestedBuilder
Expand All @@ -27,17 +29,18 @@ describe("Filters - complex cases", () => {
expect(builtQuery).toEqual(doubleNestedOrInRootAnd);
});

it("Double nested - with few attribute", () => {
it("should create nested filters with attribute", () => {
const builtQuery = new SQBuilder()
.and()
.filters("attribute1")
.with((nestedBuilder) =>
nestedBuilder
.or()
.filters("createdAt")
.lte("date1")
.filters("createdAt")
.gte("date2")
.filters("attribute1", (b) =>
b.with((nestedBuilder) =>
nestedBuilder
.or()
.filters("createdAt")
.lte("date1")
.filters("createdAt")
.gte("date2")
)
)
.filters("attribute2")
.with((nestedBuilder) =>
Expand All @@ -53,7 +56,7 @@ describe("Filters - complex cases", () => {
expect(builtQuery).toEqual(doubleNestedWithAttributes);
});

it("Complex filter with many nested filters, negations, inline building and wrong operations", () => {
it("should create complex query", () => {
const builtQuery = new SQBuilder()
.or()
.filters("attribute1")
Expand Down Expand Up @@ -105,7 +108,7 @@ describe("Filters - complex cases", () => {
)
.filters()
.with((nestedBuilder) =>
// Try attach not filter specific data
// Try to create illegal operators on nested filter operator
nestedBuilder
.or()
.filters("createdAt")
Expand Down
Loading

0 comments on commit f5b9fa3

Please sign in to comment.