Skip to content

Commit 4d6dea6

Browse files
committed
add tests for formatted tableName
1 parent 21473eb commit 4d6dea6

1 file changed

Lines changed: 119 additions & 13 deletions

File tree

test/unit/data-connect/data-connect-api-client-internal.spec.ts

Lines changed: 119 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ describe('DataConnectApiClient CRUD helpers', () => {
250250
const tableName = 'TestTable';
251251
const formatedTableName = 'testTable';
252252

253+
const dataWithUndefined = {
254+
genre: 'Action',
255+
title: 'Die Hard',
256+
ratings: null,
257+
director: {
258+
name: undefined,
259+
age: undefined
260+
},
261+
notes: undefined,
262+
releaseYear: undefined,
263+
extras: [1, undefined, 'hello', undefined, { a: 1, b: undefined }]
264+
};
265+
266+
const tableNames = ['movie', 'Movie', 'MOVIE', 'toybox', 'toyBox', 'ToyBox', 'TOYBOX'];
267+
const formatedTableNames = ['movie', 'movie', 'mOVIE', 'toybox', 'toyBox', 'toyBox', 'tOYBOX'];
268+
253269
// Helper function to normalize GraphQL strings
254270
const normalizeGraphQLString = (str: string): string => {
255271
return str
@@ -272,6 +288,15 @@ describe('DataConnectApiClient CRUD helpers', () => {
272288

273289
// --- INSERT TESTS ---
274290
describe('insert()', () => {
291+
tableNames.forEach((tableName, index) => {
292+
const expectedMutation = `mutation { ${formatedTableNames[index]}_insert(data: { name: "a" }) }`;
293+
it(`should use the formatted tableName in the gql query: "${tableName}" as "${formatedTableNames[index]}"`,
294+
async () => {
295+
await apiClient.insert(tableName, { name: 'a' });
296+
await expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
297+
});
298+
});
299+
275300
it('should call executeGraphql with the correct mutation for simple data', async () => {
276301
const simpleData = { name: 'test', value: 123 };
277302
const expectedMutation = `
@@ -299,18 +324,6 @@ describe('DataConnectApiClient CRUD helpers', () => {
299324
});
300325

301326
it('should call executeGraphql with the correct mutation for undefined and null values', async () => {
302-
const dataWithUndefined = {
303-
genre: 'Action',
304-
title: 'Die Hard',
305-
ratings: null,
306-
director: {
307-
name: undefined,
308-
age: undefined
309-
},
310-
notes: undefined,
311-
releaseYear: undefined,
312-
extras: [1, undefined, 'hello', undefined, { a: 1, b: undefined }]
313-
};
314327
const expectedMutation = `
315328
mutation {
316329
${formatedTableName}_insert(data: {
@@ -343,6 +356,15 @@ describe('DataConnectApiClient CRUD helpers', () => {
343356

344357
// --- INSERT MANY TESTS ---
345358
describe('insertMany()', () => {
359+
tableNames.forEach((tableName, index) => {
360+
const expectedMutation = `mutation { ${formatedTableNames[index]}_insertMany(data: [{ name: "a" }]) }`;
361+
it(`should use the formatted tableName in the gql query: "${tableName}" as "${formatedTableNames[index]}"`,
362+
async () => {
363+
await apiClient.insertMany(tableName, [{ name: 'a' }]);
364+
await expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
365+
});
366+
});
367+
346368
it('should call executeGraphql with the correct mutation for simple data array', async () => {
347369
const simpleDataArray = [{ name: 'test1' }, { name: 'test2', value: 456 }];
348370
const expectedMutation = `
@@ -366,6 +388,32 @@ describe('DataConnectApiClient CRUD helpers', () => {
366388
expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
367389
});
368390

391+
it('should call executeGraphql with the correct mutation for undefined and null', async () => {
392+
const dataArray = [
393+
dataWithUndefined,
394+
dataWithUndefined
395+
]
396+
const expectedMutation = `
397+
mutation {
398+
${formatedTableName}_insertMany(data: [{
399+
genre: "Action",
400+
title: "Die Hard",
401+
ratings: null,
402+
director: {},
403+
extras: [1, null, "hello", null, { a: 1 }]
404+
},
405+
{
406+
genre: "Action",
407+
title: "Die Hard",
408+
ratings: null,
409+
director: {},
410+
extras: [1, null, "hello", null, { a: 1 }]
411+
}])
412+
}`;
413+
await apiClient.insertMany(tableName, dataArray);
414+
expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
415+
});
416+
369417
it('should throw FirebaseDataConnectError for invalid tableName', async () => {
370418
await expect(apiClient.insertMany('', [{ data: 1 }]))
371419
.to.be.rejectedWith(FirebaseDataConnectError, /`tableName` must be a non-empty string./);
@@ -389,6 +437,15 @@ describe('DataConnectApiClient CRUD helpers', () => {
389437

390438
// --- UPSERT TESTS ---
391439
describe('upsert()', () => {
440+
tableNames.forEach((tableName, index) => {
441+
const expectedMutation = `mutation { ${formatedTableNames[index]}_upsert(data: { name: "a" }) }`;
442+
it(`should use the formatted tableName in the gql query: "${tableName}" as "${formatedTableNames[index]}"`,
443+
async () => {
444+
await apiClient.upsert(tableName, { name: 'a' });
445+
await expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
446+
});
447+
});
448+
392449
it('should call executeGraphql with the correct mutation for simple data', async () => {
393450
const simpleData = { id: 'key1', value: 'updated' };
394451
const expectedMutation = `mutation { ${formatedTableName}_upsert(data: { id: "key1", value: "updated" }) }`;
@@ -398,14 +455,28 @@ describe('DataConnectApiClient CRUD helpers', () => {
398455

399456
it('should call executeGraphql with the correct mutation for complex data', async () => {
400457
const complexData = { id: 'key2', active: false, items: [1, null], detail: { status: 'done/\\' } };
401-
// Note: Matching specific escaping: / -> \\, ' -> \'
402458
const expectedMutation = `
403459
mutation { ${formatedTableName}_upsert(data:
404460
{ id: "key2", active: false, items: [1, null], detail: { status: "done/\\\\" } }) }`;
405461
await apiClient.upsert(tableName, complexData);
406462
expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
407463
});
408464

465+
it('should call executeGraphql with the correct mutation for undefined and null values', async () => {
466+
const expectedMutation = `
467+
mutation {
468+
${formatedTableName}_upsert(data: {
469+
genre: "Action",
470+
title: "Die Hard",
471+
ratings: null,
472+
director: {},
473+
extras: [1, null, "hello", null, { a: 1 }]
474+
})
475+
}`;
476+
await apiClient.upsert(tableName, dataWithUndefined);
477+
expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
478+
});
479+
409480
it('should throw FirebaseDataConnectError for invalid tableName', async () => {
410481
await expect(apiClient.upsert('', { data: 1 }))
411482
.to.be.rejectedWith(FirebaseDataConnectError, /`tableName` must be a non-empty string./);
@@ -424,6 +495,15 @@ describe('DataConnectApiClient CRUD helpers', () => {
424495

425496
// --- UPSERT MANY TESTS ---
426497
describe('upsertMany()', () => {
498+
tableNames.forEach((tableName, index) => {
499+
const expectedMutation = `mutation { ${formatedTableNames[index]}_upsertMany(data: [{ name: "a" }]) }`;
500+
it(`should use the formatted tableName in the gql query: "${tableName}" as "${formatedTableNames[index]}"`,
501+
async () => {
502+
await apiClient.upsertMany(tableName, [{ name: 'a' }]);
503+
await expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
504+
});
505+
});
506+
427507
it('should call executeGraphql with the correct mutation for simple data array', async () => {
428508
const simpleDataArray = [{ id: 'k1' }, { id: 'k2', value: 99 }];
429509
const expectedMutation = `
@@ -444,6 +524,32 @@ describe('DataConnectApiClient CRUD helpers', () => {
444524
expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
445525
});
446526

527+
it('should call executeGraphql with the correct mutation for undefined and null', async () => {
528+
const dataArray = [
529+
dataWithUndefined,
530+
dataWithUndefined
531+
]
532+
const expectedMutation = `
533+
mutation {
534+
${formatedTableName}_upsertMany(data: [{
535+
genre: "Action",
536+
title: "Die Hard",
537+
ratings: null,
538+
director: {},
539+
extras: [1, null, "hello", null, { a: 1 }]
540+
},
541+
{
542+
genre: "Action",
543+
title: "Die Hard",
544+
ratings: null,
545+
director: {},
546+
extras: [1, null, "hello", null, { a: 1 }]
547+
}])
548+
}`;
549+
await apiClient.upsertMany(tableName, dataArray);
550+
expect(executeGraphqlStub).to.have.been.calledOnceWithExactly(normalizeGraphQLString(expectedMutation));
551+
});
552+
447553
it('should throw FirebaseDataConnectError for invalid tableName', async () => {
448554
expect(apiClient.upsertMany('', [{ data: 1 }]))
449555
.to.be.rejectedWith(FirebaseDataConnectError, /`tableName` must be a non-empty string./);

0 commit comments

Comments
 (0)