Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add some tests #275

Merged
merged 5 commits into from
Dec 12, 2024
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
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
Loading