11import { BaseQuery } from '../../src/lib/base-query' ;
2+ import { httpClient , AxiosInstance } from '@contentstack/core' ;
3+ import { MOCK_CLIENT_OPTIONS } from '../utils/constant' ;
4+ import MockAdapter from 'axios-mock-adapter' ;
5+ import { entryFindMock } from '../utils/mocks' ;
26
37describe ( 'BaseQuery class' , ( ) => {
48 let baseQuery : BaseQuery ;
@@ -69,4 +73,109 @@ describe('BaseQuery class', () => {
6973 baseQuery . removeParam ( 'key2' ) ;
7074 expect ( baseQuery . _queryParams ) . toEqual ( { key1 : 'value1' } ) ;
7175 } ) ;
76+ } ) ;
77+
78+ class TestableBaseQuery extends BaseQuery {
79+ constructor ( client : AxiosInstance , urlPath : string | null = null ) {
80+ super ( ) ;
81+ this . _client = client ;
82+ if ( urlPath !== null ) {
83+ this . _urlPath = urlPath ;
84+ }
85+ this . _variants = '' ;
86+ }
87+
88+ setVariants ( variants : string ) {
89+ this . _variants = variants ;
90+ }
91+
92+ setParameters ( params : any ) {
93+ this . _parameters = params ;
94+ }
95+
96+ setUrlPath ( path : string ) {
97+ this . _urlPath = path ;
98+ }
99+ }
100+
101+ describe ( 'BaseQuery find method' , ( ) => {
102+ let client : AxiosInstance ;
103+ let mockClient : MockAdapter ;
104+ let query : TestableBaseQuery ;
105+
106+ beforeAll ( ( ) => {
107+ client = httpClient ( MOCK_CLIENT_OPTIONS ) ;
108+ mockClient = new MockAdapter ( client as any ) ;
109+ } ) ;
110+
111+ beforeEach ( ( ) => {
112+ query = new TestableBaseQuery ( client , '/content_types/test_uid/entries' ) ;
113+ mockClient . reset ( ) ;
114+ } ) ;
115+
116+ it ( 'should call find with encode parameter true' , async ( ) => {
117+ mockClient . onGet ( '/content_types/test_uid/entries' ) . reply ( 200 , entryFindMock ) ;
118+
119+ query . setParameters ( { title : 'Test' } ) ;
120+ const result = await query . find ( true ) ;
121+
122+ expect ( result ) . toEqual ( entryFindMock ) ;
123+ } ) ;
124+
125+ it ( 'should call find without parameters' , async ( ) => {
126+ mockClient . onGet ( '/content_types/test_uid/entries' ) . reply ( 200 , entryFindMock ) ;
127+
128+ const result = await query . find ( ) ;
129+
130+ expect ( result ) . toEqual ( entryFindMock ) ;
131+ } ) ;
132+
133+ it ( 'should call find with variants header when variants are set' , async ( ) => {
134+ mockClient . onGet ( '/content_types/test_uid/entries' ) . reply ( ( config ) => {
135+ expect ( config . headers ?. [ 'x-cs-variant-uid' ] ) . toBe ( 'variant1,variant2' ) ;
136+ return [ 200 , entryFindMock ] ;
137+ } ) ;
138+
139+ query . setVariants ( 'variant1,variant2' ) ;
140+ await query . find ( ) ;
141+ } ) ;
142+
143+ it ( 'should extract content type UID from URL path' , async ( ) => {
144+ mockClient . onGet ( '/content_types/my_content_type/entries' ) . reply ( 200 , entryFindMock ) ;
145+
146+ const queryWithContentType = new TestableBaseQuery ( client , '/content_types/my_content_type/entries' ) ;
147+ const result = await queryWithContentType . find ( ) ;
148+
149+ expect ( result ) . toEqual ( entryFindMock ) ;
150+ } ) ;
151+
152+ it ( 'should return null for content type UID when URL does not match pattern' , async ( ) => {
153+ mockClient . onGet ( '/assets' ) . reply ( 200 , entryFindMock ) ;
154+
155+ const queryWithoutContentType = new TestableBaseQuery ( client , '/assets' ) ;
156+ const result = await queryWithoutContentType . find ( ) ;
157+
158+ expect ( result ) . toEqual ( entryFindMock ) ;
159+ } ) ;
160+
161+ it ( 'should handle find with both encode and variants' , async ( ) => {
162+ mockClient . onGet ( '/content_types/test_uid/entries' ) . reply ( ( config ) => {
163+ expect ( config . headers ?. [ 'x-cs-variant-uid' ] ) . toBe ( 'test-variant' ) ;
164+ return [ 200 , entryFindMock ] ;
165+ } ) ;
166+
167+ query . setVariants ( 'test-variant' ) ;
168+ query . setParameters ( { status : 'published' } ) ;
169+ const result = await query . find ( true ) ;
170+
171+ expect ( result ) . toEqual ( entryFindMock ) ;
172+ } ) ;
173+
174+ it ( 'should handle empty _urlPath gracefully' , ( ) => {
175+ const queryWithoutUrlPath = new TestableBaseQuery ( client , null ) ;
176+ queryWithoutUrlPath . setUrlPath ( '' ) ;
177+
178+ // Verify that URL path is empty (testing the null check in extractContentTypeUidFromUrl)
179+ expect ( queryWithoutUrlPath ) . toBeInstanceOf ( TestableBaseQuery ) ;
180+ } ) ;
72181} ) ;
0 commit comments