Skip to content

Commit 190cb22

Browse files
committed
test: added tests for regex and other query methods
1 parent 9d34565 commit 190cb22

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

test/unit/query.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,82 @@ describe('Query class', () => {
8383
expect(mainQuery2._parameters).toHaveProperty('$and', [subQuery1._parameters, subQuery2._parameters]);
8484
});
8585

86+
it('should result in error when regex method is called with invalid regex', async () => {
87+
const regexQuery = getQueryObject(client, 'your-referenced-content-type-uid');
88+
expect(() => regexQuery.regex("fieldUid", "[a-z")).toThrow("Invalid regexPattern: Must be a valid regular expression");
89+
});
90+
91+
it('should add a regex parameter to _parameters when regex method is called with valid regex', () => {
92+
query.regex('fieldUid', '^ABCXYZ123');
93+
expect(query._parameters['fieldUid']).toEqual({ $regex: '^ABCXYZ123' });
94+
});
95+
96+
it('should add a containedIn parameter to _parameters', () => {
97+
query.containedIn('fieldUid', ['value1', 'value2']);
98+
expect(query._parameters['fieldUid']).toEqual({ '$in': ['value1', 'value2'] });
99+
});
100+
101+
it('should add a notContainedIn parameter to _parameters', () => {
102+
query.notContainedIn('fieldUid', ['value1', 'value2']);
103+
expect(query._parameters['fieldUid']).toEqual({ '$nin': ['value1', 'value2'] });
104+
});
105+
106+
it('should add an exists parameter to _parameters', () => {
107+
query.exists('fieldUid');
108+
expect(query._parameters['fieldUid']).toEqual({ '$exists': true });
109+
});
110+
111+
it('should add a notExists parameter to _parameters', () => {
112+
query.notExists('fieldUid');
113+
expect(query._parameters['fieldUid']).toEqual({ '$exists': false });
114+
});
115+
116+
it('should add an equalTo parameter to _parameters', () => {
117+
query.equalTo('fieldUid', 'value');
118+
expect(query._parameters['fieldUid']).toEqual('value');
119+
});
120+
121+
it('should add a notEqualTo parameter to _parameters', () => {
122+
query.notEqualTo('fieldUid', 'value');
123+
expect(query._parameters['fieldUid']).toEqual({ '$ne': 'value' });
124+
});
125+
126+
it('should add a lessThan parameter to _parameters', () => {
127+
query.lessThan('fieldUid', 10);
128+
expect(query._parameters['fieldUid']).toEqual({ '$lt': 10 });
129+
});
130+
131+
it('should add a lessThanOrEqualTo parameter to _parameters', () => {
132+
query.lessThanOrEqualTo('fieldUid', 10);
133+
expect(query._parameters['fieldUid']).toEqual({ '$lte': 10 });
134+
});
135+
136+
it('should add a greaterThan parameter to _parameters', () => {
137+
query.greaterThan('fieldUid', 10);
138+
expect(query._parameters['fieldUid']).toEqual({ '$gt': 10 });
139+
});
140+
141+
it('should add a greaterThanOrEqualTo parameter to _parameters', () => {
142+
query.greaterThanOrEqualTo('fieldUid', 10);
143+
expect(query._parameters['fieldUid']).toEqual({ '$gte': 10 });
144+
});
145+
146+
it('should add a tags parameter to _parameters', () => {
147+
query.tags(['tag1', 'tag2']);
148+
expect(query._parameters['tags']).toEqual(['tag1', 'tag2']);
149+
});
150+
151+
it('should add a search parameter to _queryParams', () => {
152+
query.search('searchKey');
153+
expect(query._queryParams['typeahead']).toEqual('searchKey');
154+
});
155+
156+
it('should provide proper response when find method is called', async () => {
157+
mockClient.onGet(`/content_types/contentTypeUid/entries`).reply(200, entryFindMock);
158+
const returnedValue = await query.find();
159+
expect(returnedValue).toEqual(entryFindMock);
160+
});
161+
86162
it('should provide proper response when find method is called', async () => {
87163
mockClient.onGet(`/content_types/contentTypeUid/entries`).reply(200, entryFindMock);
88164
const returnedValue = await query.find();

0 commit comments

Comments
 (0)