Skip to content

Commit

Permalink
Improve tests for PUT gristlabs#601
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent authored and Florent F committed Aug 1, 2023
1 parent 98b5acf commit cffce13
Showing 1 changed file with 74 additions and 78 deletions.
152 changes: 74 additions & 78 deletions test/server/lib/DocApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,97 +844,93 @@ function testDocApi() {

describe("PUT /docs/{did}/columns", function () {

it('should create new columns and update existing ones', async function () {
// given
async function init() {
const wid = (await userApi.getOrgWorkspaces('current')).find((w) => w.name === 'Private')!.id;
const docId = await userApi.newDoc({name: 'ColumnsPut'}, wid);
const url = `${serverUrl}/api/docs/${docId}/tables/Table1/columns`;

{
const body: ColumnsPut = {
columns: [{
id: "Foo",
fields: {
type: "Text",
label: "FooLabel"
}
}]
};
// when
const resp = await axios.put(url, body, chimpy);

// then
assert.equal(resp.status, 200);
}
return `${serverUrl}/api/docs/${docId}/tables/Table1/columns`;
}

{
// when
const resp = await axios.get(url, chimpy);
async function getColumnFieldsMapById(url: string) {
const result = await axios.get(url, chimpy);
assert.equal(result.status, 200);
return new Map(
result.data.columns.map(
({id, fields}: {id: string, fields: object}) => [id, fields]
)
)
}

// then
assert.equal(resp.status, 200);
const fooColumn = resp.data.columns.find((col: {id: string}) => col.id === "Foo");
assert.exists(fooColumn, 'Expecting to have a column with id "Foo"');
assert.deepInclude(fooColumn.fields, { type: "Text", label: "FooLabel" });
const FOO_COLUMN_DESCRIPTION = {
id: "Foo",
fields: {
type: "Text",
label: "FooLabel"
}
};

{
const NEW_COLUMN_ID = "Bar";
const EXISTING_COLUMN_ID = "Foo";
const NEW_EXISTING_COLUMN_ID = "FooNew";
const body: ColumnsPut = {
columns: [{
id: NEW_COLUMN_ID,
fields: {
type: "Text",
label: "Bar",
}
}, {
id: EXISTING_COLUMN_ID,
fields: {
type: "Numeric",
colId: NEW_EXISTING_COLUMN_ID
}
}]
};

// when
const resp = await axios.put(url, body, chimpy);
it('should create new columns', async function () {

// then
assert.equal(resp.status, 200);
}
// given
const url = await init();
const body: ColumnsPut = {
columns: [FOO_COLUMN_DESCRIPTION]
};
// when
const resp = await axios.put(url, body, chimpy);
// then
const columnFieldsMap = await getColumnFieldsMapById(url);
assert.equal(resp.status, 200);

{
// when
const resp = await axios.get(url, chimpy);
assert.isTrue(columnFieldsMap.has(FOO_COLUMN_DESCRIPTION.id), 'Expecting to have a column with id "Foo"');
assert.deepInclude(columnFieldsMap.get(FOO_COLUMN_DESCRIPTION.id), { type: "Text", label: "FooLabel" });
});

// then
assert.equal(resp.status, 200);
const fieldsByColId = new Map(
resp.data.columns.map(
({id, fields}: {id: string, fields: object}) => [id, fields]
)
);
it('should update existing columns and create new ones', async function () {
// given
const url = await init();
const NEW_COLUMN_ID = "Bar";
const EXISTING_COLUMN_ID = "A";
const EXISTING_COLUMN_LABEL = "A";
const UPDATED_EXISTING_COLUMN_ID = "NewA";
const submittedColumns: ColumnsPut = {
columns: [{
id: NEW_COLUMN_ID,
fields: {
type: "Text",
label: "Bar",
}
}, {
id: EXISTING_COLUMN_ID,
fields: {
type: "Numeric",
colId: UPDATED_EXISTING_COLUMN_ID
}
}]
};

const fooNewFields = fieldsByColId.get("FooNew");
assert.exists(fooNewFields, 'Expecting to have a column with id "FooNew"');
assert.deepInclude(
fooNewFields,
{ type: "Numeric", label: "FooLabel" },
"Expecting to have type changed to Numeric and label unchanged"
);
// when
const resp = await axios.put(url, submittedColumns, chimpy);

assert.isFalse(
fieldsByColId.has("Foo"),
'Expecting to not have a column with id "Foo" anymore as the ID has been renamed'
);
// then
assert.equal(resp.status, 200);
const fieldsByColId = await getColumnFieldsMapById(url);

const updatedColFields = fieldsByColId.get(UPDATED_EXISTING_COLUMN_ID);
assert.exists(updatedColFields, `Expecting to have a column with id "${NEW_COLUMN_ID}"`);
assert.deepInclude(
updatedColFields,
{ type: "Numeric", label: EXISTING_COLUMN_LABEL },
"Expecting to have type changed to Numeric and label unchanged"
);

const barFields = fieldsByColId.get('Bar');
assert.exists(barFields, 'Bar column should have been added');
assert.deepInclude(barFields, { type: "Text", label: "Bar" });
assert.isFalse(
fieldsByColId.has(EXISTING_COLUMN_ID),
`Expecting to not have a column with id "${EXISTING_COLUMN_ID}" anymore as the ID has been renamed`
);

}
const newColFields = fieldsByColId.get(NEW_COLUMN_ID);
assert.exists(newColFields, `Column with id "${NEW_COLUMN_ID}" should have been added`);
assert.deepInclude(newColFields, { type: "Text", label: "Bar" });
});
});

Expand Down

0 comments on commit cffce13

Please sign in to comment.