Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion schemas/article/RawArticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { getRandomNumberAsString } from '@/functions/utils/randomDefaultValues'
*/
export const RawArticleSchema = z.object({
article_id: z.string().default(getRandomNumberAsString()).optional(),
article_category_id: z.number().optional(),
article_category_id: z
.number()
.transform((val) => (val === null ? undefined : val))
.optional(),
article_name: z.string().default('Article-XY'),
article_code: z.string().default(getRandomNumberAsString()).optional(),
article_eanCode: z.string().default(getRandomNumberAsString()).optional(),
Expand Down
3 changes: 3 additions & 0 deletions schemas/utils/schemaDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export default function schemaDefaults<Schema extends z.ZodFirstPartySchemaTypes
case z.ZodFirstPartyTypeKind.ZodCatch:
return schema._def.catchValue.call(null, {} as any)

case z.ZodFirstPartyTypeKind.ZodEffects:
return schemaDefaults((schema as any)._def.schema, options)

default:
throw new Error(`Unsupported type ${(schema as any)._def.typeName}`)
}
Expand Down
11 changes: 9 additions & 2 deletions tests/api/Get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import GET from '@/api/GET'
import { setAuthorization } from '@/config/authorization'
import { RawInvoices, safeParseRawInvoices } from '@/schemas/invoice/RawInvoices'
import { RawArticles, safeParseRawArticles } from '@/schemas/article/RawArticles'
import { RawCustomers, safeParseRawCustomers } from '@/schemas/customer/RawCustomers'

beforeEach(() => {
setAuthorization(process.env.AUTH_TOKEN!)
Expand All @@ -15,14 +16,20 @@ describe('Testing #GET function: ', () => {
expect(safeParseRawInvoices(response).success).toBe(true)
})

test('articles endpoint should return RawInvoices object', async () => {
const response = await GET<RawArticles>('articles')
test('articles endpoint should return RawArticles object', async () => {
const response = await GET<RawArticles>('articles', [`limit=${5}`])

expect(safeParseRawArticles(response).success).toBe(true)
expect(response.articles.length).toBeGreaterThanOrEqual(5)
})

test('calling without filter params should yield results anyway', async () => {
const articles = await GET<RawArticles>('articles')
expect(articles.articles.length).toBeGreaterThan(0)
})

test('validity of customer schema against response of /users endpoint', async () => {
const customers = await GET<RawCustomers>('users')
expect(safeParseRawCustomers(customers).success).toBe(true)
})
})
15 changes: 5 additions & 10 deletions tests/invoices/getInvoices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,20 @@ test('getInvoices - limit of 10, Expect 10 invoices', async () => {
expect(invoices.length).toBeLessThanOrEqual(10)
})

test('getInvoices - limit of 0, Expect 0 invoices', async () => {
const invoices = await getInvoices(0)
expect(invoices).toHaveLength(0)
})

test(
'getInvoices - limit of 1000, Expect at least 0 invoices',
'getInvoices - limit of 1000 expect at least 0 invoices',
async () => {
const invoices = await getInvoices(1000)
expect(Array.isArray(invoices)).toBe(true)
expect(invoices.length).toBeGreaterThan(0)
expect(invoices.length).toBeGreaterThanOrEqual(0)
},
10 * 1000,
)

test('getInvoices - limit of -1, Expect at least 0 invoices', async () => {
const invoices = await getInvoices(-1)
test('getInvoices - default limit expect array of invoices', async () => {
const invoices = await getInvoices()
expect(Array.isArray(invoices)).toBe(true)
expect(invoices.length).toBeGreaterThan(0)
expect(invoices.length).toBeGreaterThanOrEqual(0)
})

test('getInvoices - No Authentication set; Expect Authentication Failure', async () => {
Expand Down