Add tests for User validations

This commit is contained in:
roschaefer 2019-09-20 20:02:51 +02:00
parent b4ffa13517
commit 4fbabc879f
2 changed files with 42 additions and 1 deletions

View File

@ -4,7 +4,7 @@ module.exports = {
id: { type: 'string', primary: true, default: uuid }, // TODO: should be type: 'uuid' but simplified for our tests
actorId: { type: 'string', allow: [null] },
name: { type: 'string', disallow: [null], min: 3 },
slug: 'string',
slug: { type: 'string', regex: /^[a-z0-9_-]+$/, lowercase: true },
encryptedPassword: 'string',
avatar: { type: 'string', allow: [null] },
coverImg: { type: 'string', allow: [null] },

View File

@ -18,3 +18,44 @@ describe('role', () => {
)
})
})
describe('slug', () => {
it('normalizes to lowercase letters', async () => {
const user = await instance.create('User', { slug: 'Matt' })
await expect(user.toJson()).resolves.toEqual(
expect.objectContaining({
slug: 'matt',
}),
)
})
describe('characters', () => {
const createUser = attrs => {
return instance.create('User', attrs).then(user => user.toJson())
}
it('-', async () => {
await expect(createUser({ slug: 'matt-rider' })).resolves.toMatchObject({
slug: 'matt-rider',
})
})
it('_', async () => {
await expect(createUser({ slug: 'matt_rider' })).resolves.toMatchObject({
slug: 'matt_rider',
})
})
it(' ', async () => {
await expect(createUser({ slug: 'matt rider' })).rejects.toThrow(
/fails to match the required pattern/,
)
})
it('ä', async () => {
await expect(createUser({ slug: 'mätt' })).rejects.toThrow(
/fails to match the required pattern/,
)
})
})
})