Skip to content

Commit

Permalink
Change naming of the useCraftUrlBuilder to buildCraftQueryUrl.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelreichor committed Oct 29, 2024
1 parent 56b2a6e commit 8378e3d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-apricots-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'js-craftcms-api': patch
---

Change naming of the useCraftUrlBuilder to buildCraftQueryUrl
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export interface QueryBuilderMap {
}

// Generic implementation of the function
export function useCraftUrlBuilder<T extends ElementType>(elementType: T): QueryBuilderMap[T] {
export function buildCraftQueryUrl<T extends ElementType>(elementType: T): QueryBuilderMap[T] {
const defaultParams: MergedQueryParams = {
elementType: 'entries',
};
Expand Down
36 changes: 18 additions & 18 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useCraftUrlBuilder } from '../index';
import { buildCraftQueryUrl } from '../index';
import { describe, it, expect } from 'vitest';
import type { ElementType, ExecutionMethods } from '../index';

describe('useQueryBuilder Tests', () => {
describe('useQueryBuilder - CommonQueryBuilder', () => {
describe('buildCraftQueryUrl Tests', () => {
describe('buildCraftQueryUrl - CommonQueryBuilder', () => {
const arrOfExecutionTypes: ExecutionMethods[] = ['one', 'all'];
const arrOfElementTypes: ElementType[] = ['addresses', 'assets', 'entries', 'users'];

arrOfElementTypes.forEach((elementType) => {
const queryBuilder = useCraftUrlBuilder(elementType);
const queryBuilder = buildCraftQueryUrl(elementType);
const baseQuery = queryBuilder
.id(1)
.limit(5)
Expand All @@ -29,9 +29,9 @@ describe('useQueryBuilder Tests', () => {
});
});

describe('useQueryBuilder - AddressQueryBuilder', () => {
describe('buildCraftQueryUrl - AddressQueryBuilder', () => {
it('Should execute all addressQueryBuilder functions', async () => {
const queryBuilder = useCraftUrlBuilder('addresses');
const queryBuilder = buildCraftQueryUrl('addresses');
const queryUrl = queryBuilder
.addressLine1('123 Main St')
.addressLine2('123')
Expand All @@ -46,9 +46,9 @@ describe('useQueryBuilder Tests', () => {
});
});

describe('useQueryBuilder - AssetQueryBuilder', () => {
describe('buildCraftQueryUrl - AssetQueryBuilder', () => {
it('Should execute all assetQueryBuilder functions', async () => {
const queryBuilder = useCraftUrlBuilder('assets');
const queryBuilder = buildCraftQueryUrl('assets');
const queryUrl = queryBuilder
.volume('images')
.kind('png')
Expand All @@ -63,9 +63,9 @@ describe('useQueryBuilder Tests', () => {
});
});

describe('useQueryBuilder - EntryQueryBuilder', () => {
describe('buildCraftQueryUrl - EntryQueryBuilder', () => {
it('Should execute all entryQueryBuilder functions', async () => {
const queryBuilder = useCraftUrlBuilder('entries');
const queryBuilder = buildCraftQueryUrl('entries');
const queryUrl = queryBuilder
.slug('my-slug')
.uri(['news', '2023'])
Expand All @@ -81,9 +81,9 @@ describe('useQueryBuilder Tests', () => {
});
});

describe('useQueryBuilder - UserQueryBuilder', () => {
describe('buildCraftQueryUrl - UserQueryBuilder', () => {
it('Should execute all userQueryBuilder functions', async () => {
const queryBuilder = useCraftUrlBuilder('users');
const queryBuilder = buildCraftQueryUrl('users');
const queryUrl = queryBuilder
.group('admins')
.groupId(1)
Expand All @@ -98,9 +98,9 @@ describe('useQueryBuilder Tests', () => {
});
});

describe('useQueryBuilder - Edge cases', () => {
describe('buildCraftQueryUrl - Edge cases', () => {
it('Should handle null, empty strings, and undefined gracefully', async () => {
const queryBuilder = useCraftUrlBuilder('entries');
const queryBuilder = buildCraftQueryUrl('entries');

// @ts-ignore: Is for testing
const nullQueryUrl = queryBuilder.slug(null).buildBaseUrl('one');
Expand All @@ -117,7 +117,7 @@ describe('useQueryBuilder Tests', () => {
});

it('Should handle special characters in parameters correctly', async () => {
const queryBuilder = useCraftUrlBuilder('entries');
const queryBuilder = buildCraftQueryUrl('entries');

const spacesQueryUrl = queryBuilder.slug('my slug with spaces').buildBaseUrl('one');
const specialCharsQueryUrl = queryBuilder
Expand All @@ -133,18 +133,18 @@ describe('useQueryBuilder Tests', () => {
});

it('Should handle multiple values for the same parameter (arrays)', async () => {
const queryBuilder = useCraftUrlBuilder('entries');
const queryBuilder = buildCraftQueryUrl('entries');
const queryUrl = queryBuilder.uri(['news', '2023', 'sports', '']).buildBaseUrl('one');

// Ensure the query string correctly joins the array
expect(queryUrl).toContain('elementType=entries&uri=news%2F2023%2Fsports&one=1');
});
});

describe('useQueryBuilder - Invalid element type', () => {
describe('buildCraftQueryUrl - Invalid element type', () => {
it('Should throw an error when an invalid elementType is provided', () => {
expect(() => {
useCraftUrlBuilder('invalidType' as any);
buildCraftQueryUrl('invalidType' as any);
}).toThrowError('Unsupported element type: invalidType');
});
});
Expand Down

0 comments on commit 8378e3d

Please sign in to comment.