@@ -12,6 +12,7 @@ jest.mock('../../src/lib/openops-tables/create-axios-headers', () => ({
1212import { EncryptedObject } from '@openops/shared' ;
1313import { TablesServerContext } from '../../src/lib/openops-tables/context-helpers' ;
1414import {
15+ getTableById ,
1516 getTableByName ,
1617 getTableIdByTableName ,
1718 getTableNames ,
@@ -176,6 +177,46 @@ describe('get table id by table name', () => {
176177 } ) ;
177178} ) ;
178179
180+ describe ( 'get table by id' , ( ) => {
181+ beforeEach ( ( ) => {
182+ jest . clearAllMocks ( ) ;
183+ } ) ;
184+
185+ test ( 'should return the matching table' , async ( ) => {
186+ makeOpenOpsTablesGetMock . mockResolvedValue ( [
187+ { id : 1 , name : 'table name 1' } ,
188+ { id : 2 , name : 'table name 2' } ,
189+ ] ) ;
190+ createAxiosHeadersMock . mockReturnValue ( 'some header' ) ;
191+
192+ const result = await getTableById ( 2 , mockTablesServerContext ) ;
193+
194+ expect ( result ) . toStrictEqual ( { id : 2 , name : 'table name 2' } ) ;
195+ expect ( makeOpenOpsTablesGetMock ) . toBeCalledTimes ( 1 ) ;
196+ expect ( makeOpenOpsTablesGetMock ) . toHaveBeenCalledWith (
197+ 'api/database/tables/database/1/' ,
198+ 'some header' ,
199+ ) ;
200+ expect ( createAxiosHeadersMock ) . toBeCalledTimes ( 1 ) ;
201+ expect ( createAxiosHeadersMock ) . toHaveBeenCalledWith ( {
202+ getToken : expect . any ( Function ) ,
203+ } ) ;
204+ } ) ;
205+
206+ test ( 'should return undefined if table id is not found' , async ( ) => {
207+ makeOpenOpsTablesGetMock . mockResolvedValue ( [
208+ { id : 1 , name : 'table name 1' } ,
209+ ] ) ;
210+ createAxiosHeadersMock . mockReturnValue ( 'some header' ) ;
211+
212+ const result = await getTableById ( 99 , mockTablesServerContext ) ;
213+
214+ expect ( result ) . toBeUndefined ( ) ;
215+ expect ( makeOpenOpsTablesGetMock ) . toBeCalledTimes ( 1 ) ;
216+ expect ( createAxiosHeadersMock ) . toBeCalledTimes ( 1 ) ;
217+ } ) ;
218+ } ) ;
219+
179220describe ( 'get table by table name' , ( ) => {
180221 beforeEach ( ( ) => {
181222 jest . clearAllMocks ( ) ;
0 commit comments