@applica-software-guru/crud-client
Version:
Libreria per l'accesso ai servizi REST di Applica.
104 lines (89 loc) • 3.18 kB
text/typescript
import { PASSWORD, USERNAME, createProviders, createRandomString } from './config';
import { describe, expect, it } from 'vitest';
describe('test updateMany', async () => {
const { dataProvider, authProvider } = createProviders();
it('should fail when not logged in', async () => {
expect(
dataProvider.updateMany('entities/category', {
ids: [1, 2, 3],
data: {},
rows: []
})
).rejects.toThrow('iam.error.unauthorized');
});
it('should fail with validation errors', async () => {
await authProvider.login({ username: USERNAME, password: PASSWORD });
const category = await dataProvider.create('entities/category', {
data: {
description: { it: createRandomString(10) }
}
});
expect(
dataProvider.updateMany('entities/category', {
ids: [category.data?.id],
data: {
description: {}
},
rows: [category?.data]
})
).rejects.toThrow('error.validation');
});
it('should success with valid data', async () => {
await authProvider.login({ username: USERNAME, password: PASSWORD });
const category = {
description: { it: createRandomString(10) }
};
const { data } = await dataProvider.create('entities/category', {
data: category
});
expect(data).toBeDefined();
expect(data?.id).toBeDefined();
expect(data?.description?.it).toBe(category.description?.it);
const updatedCategory = {
...data,
description: { it: createRandomString(10) }
};
const { data: updated } = await dataProvider.update('entities/category', {
id: data?.id,
data: updatedCategory,
previousData: data
});
expect(updated).toBeDefined();
expect(updated?.id).toBeDefined();
expect(updated?.description?.it).toBe(updatedCategory.description?.it);
// Create 3 categories and update 2 of them:
const category1 = await dataProvider.create('entities/category', {
data: {
description: { it: createRandomString(10) }
}
});
const category2 = await dataProvider.create('entities/category', {
data: {
description: { it: createRandomString(10) }
}
});
const category3 = await dataProvider.create('entities/category', {
data: {
description: { it: createRandomString(10) }
}
});
expect(category1).toBeDefined();
expect(category1?.data?.id).toBeDefined();
expect(category2).toBeDefined();
expect(category2?.data?.id).toBeDefined();
expect(category3).toBeDefined();
expect(category3?.data?.id).toBeDefined();
const newDescription = createRandomString(10);
const { data: updatedEntries } = await dataProvider.updateMany('entities/category', {
ids: [category1?.data?.id, category2?.data?.id],
data: {
description: { it: newDescription }
},
rows: [category1?.data, category2?.data]
});
expect(updatedEntries).toBeDefined();
expect(updatedEntries?.length).toBe(2);
expect(updatedEntries?.[0]?.description?.it).toBe(newDescription);
expect(updatedEntries?.[1]?.description?.it).toBe(newDescription);
});
});