Skip to content

Commit

Permalink
test: add some tests (#275)
Browse files Browse the repository at this point in the history
* test: add tests for zod schemas

* test: add tests on loaders

* test: add tests on auth

* test: add tests on event sender

* test: add tests on fetcher & axios errors handling
  • Loading branch information
QRuhier authored Dec 12, 2024
1 parent 0c4bfb5 commit 13e889f
Show file tree
Hide file tree
Showing 19 changed files with 1,247 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from 'vitest'
import { localStorageObjectSchema } from './localSyncObjectSchema'

describe('localStorageObjectSchema', () => {
it('should validate a valid object', () => {
const validData = {
error: false,
surveyUnitsSuccess: ['unit1', 'unit2'],
surveyUnitsInTempZone: ['unit3', 'unit4'],
}

const result = localStorageObjectSchema.safeParse(validData)
expect(result.success).toBe(true)
expect(result.success && result.data).toEqual(validData)
})

it('should fail validation when "error" is not a boolean', () => {
const invalidData = {
error: 'notABoolean',
surveyUnitsSuccess: ['unit1'],
surveyUnitsInTempZone: ['unit2'],
}

const result = localStorageObjectSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Expected boolean')
}
})

it('should fail validation when "surveyUnitsSuccess" is not an array of strings', () => {
const invalidData = {
error: true,
surveyUnitsSuccess: [123],
surveyUnitsInTempZone: ['unit2'],
}

const result = localStorageObjectSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Expected string')
}
})

it('should fail validation when "surveyUnitsInTempZone" is missing', () => {
const invalidData = {
error: false,
surveyUnitsSuccess: ['unit1', 'unit2'],
}

const result = localStorageObjectSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Required')
}
})
})
52 changes: 52 additions & 0 deletions src/core/adapters/queenApi/parserSchema/campaignSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, it } from 'vitest'
import { campaignSchema } from './campaignSchema'

describe('campaignSchema', () => {
it('should validate a valid campaign object', () => {
const validData = {
id: 'campaign1',
questionnaireIds: ['q1', 'q2', 'q3'],
}

const result = campaignSchema.safeParse(validData)
expect(result.success).toBe(true)
expect(result.success && result.data).toEqual(validData)
})

it('should fail validation when "id" is missing', () => {
const invalidData = {
questionnaireIds: ['q1', 'q2'],
}

const result = campaignSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Required')
}
})

it('should fail validation when "questionnaireIds" is not an array of strings', () => {
const invalidData = {
id: 'campaign2',
questionnaireIds: [123, 'q2'],
}

const result = campaignSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Expected string')
}
})

it('should fail validation when "questionnaireIds" is missing', () => {
const invalidData = {
id: 'campaign3',
}

const result = campaignSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Required')
}
})
})
84 changes: 84 additions & 0 deletions src/core/adapters/queenApi/parserSchema/nomenclatureSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, it, expect } from 'vitest'
import {
nomenclatureSchema,
requiredNomenclaturesSchema,
} from './nomenclatureSchema'

describe('nomenclatureSchema', () => {
it('should validate a valid array of nomenclature objects', () => {
const validData = [
{ id: 'n1', label: 'Label 1', extraField: 'extraValue1' },
{ id: 'n2', label: 'Label 2', anotherExtra: 'extraValue2' },
]

const result = nomenclatureSchema.safeParse(validData)
expect(result.success).toBe(true)
expect(result.success && result.data).toEqual(validData)
})

it('should fail validation when "id" is missing in one object', () => {
const invalidData = [
{ label: 'Label 1', extraField: 'extraValue1' },
{ id: 'n2', label: 'Label 2' },
]

const result = nomenclatureSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Required')
}
})

it('should fail validation when "label" is missing in one object', () => {
const invalidData = [
{ id: 'n1', extraField: 'extraValue1' },
{ id: 'n2', label: 'Label 2' },
]

const result = nomenclatureSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Required')
}
})

it('should fail validation when an object is not valid', () => {
const invalidData = [{ id: 'n1', label: 123 }]

const result = nomenclatureSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Expected string')
}
})
})

describe('requiredNomenclaturesSchema', () => {
it('should validate a valid array of strings', () => {
const validData = ['n1', 'n2', 'n3']

const result = requiredNomenclaturesSchema.safeParse(validData)
expect(result.success).toBe(true)
expect(result.success && result.data).toEqual(validData)
})

it('should fail validation when an element is not a string', () => {
const invalidData = ['n1', 123, 'n3']

const result = requiredNomenclaturesSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Expected string')
}
})

it('should fail validation when the value is not an array', () => {
const invalidData = 'notAnArray'

const result = requiredNomenclaturesSchema.safeParse(invalidData)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Expected array')
}
})
})
134 changes: 134 additions & 0 deletions src/core/adapters/queenApi/parserSchema/paradataSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { describe, expect, it } from 'vitest'
import { paradataSchema } from './paradataSchema'

describe('eventSchema', () => {
const eventSchema = paradataSchema.shape.event.element
const validEvent = {
type: 'click',
timestamp: 1634567890,
userAgent: 'Mozilla/5.0',
idSurveyUnit: 'su123',
idOrchestrator: 'orchestrator-collect',
idQuestionnaire: 'q123',
idParadataObject: 'po123',
typeParadataObject: 'orchestrator',
page: null,
}

it('should validate a valid event object', () => {
const result = eventSchema.safeParse(validEvent)
expect(result.success).toBe(true)
expect(result.success && result.data).toEqual(validEvent)
})

it('should fail validation for invalid "type"', () => {
const invalidEvent = {
...validEvent,
type: 'invalid-type',
}

const result = eventSchema.safeParse(invalidEvent)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain(
"Invalid enum value. Expected 'click' | 'session-started' | 'orchestrator-create', received 'invalid-type'"
)
}
})

it('should fail validation for negative timestamp', () => {
const invalidEvent = {
...validEvent,
timestamp: -1,
}

const result = eventSchema.safeParse(invalidEvent)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain(
'Number must be greater than or equal to 0'
)
}
})

it('should fail validation for missing required properties', () => {
const invalidEvent = {
type: 'click',
timestamp: 1634567890,
userAgent: 'Mozilla/5.0',
}

const result = eventSchema.safeParse(invalidEvent)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Required')
}
})
})

describe('paradataSchema', () => {
it('should validate a valid paradata object', () => {
const validParadata = {
idSu: 'su123',
event: [
{
type: 'click',
timestamp: 1634567890,
userAgent: 'Mozilla/5.0',
idSurveyUnit: 'su123',
idOrchestrator: 'orchestrator-collect',
idQuestionnaire: 'q123',
idParadataObject: 'po123',
typeParadataObject: 'orchestrator',
page: null,
},
],
}

const result = paradataSchema.safeParse(validParadata)
expect(result.success).toBe(true)
expect(result.success && result.data).toEqual(validParadata)
})

it('should fail validation for missing "idSu"', () => {
const invalidParadata = {
event: [
{
type: 'click',
timestamp: 1634567890,
userAgent: 'Mozilla/5.0',
idSurveyUnit: 'su123',
idOrchestrator: 'orchestrator-collect',
idQuestionnaire: 'q123',
idParadataObject: 'po123',
typeParadataObject: 'orchestrator',
page: null,
},
],
}

const result = paradataSchema.safeParse(invalidParadata)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Required')
}
})

it('should fail validation for invalid event array', () => {
const invalidParadata = {
idSu: 'su123',
event: [
{
type: 'invalid-type',
timestamp: -1,
},
],
}

const result = paradataSchema.safeParse(invalidParadata)
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues[0].message).toContain('Invalid enum value')
}
})
})
Loading

0 comments on commit 13e889f

Please sign in to comment.