Skip to content

Commit

Permalink
Fix editing multi-sheets (#183)
Browse files Browse the repository at this point in the history
Fixes: #182
  • Loading branch information
bosschaert committed Jun 26, 2024
1 parent 36a86eb commit 673f5f4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
4 changes: 2 additions & 2 deletions blocks/sheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function getSheetData(sheetData) {
return [header, ...data];
}

async function getData(url) {
export async function getData(url) {
const resp = await daFetch(url);
if (!resp.ok) return getDefaultSheet();

Expand All @@ -51,7 +51,7 @@ async function getData(url) {
const names = json[':names'];

// Single sheet
if (json.data) {
if (json[':type'] === 'sheet') {
const data = getSheetData(json.data);
const columns = data[0].map(() => ({ width: '300px' }));
const dataSheet = {
Expand Down
93 changes: 93 additions & 0 deletions test/unit/blocks/sheet/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { expect } from '@esm-bundle/chai';

// This is needed to make a dynamic import work that is indirectly referenced
// from blocks/sheet/index.js
const { setNx } = await import('../../../../scripts/utils.js');
setNx('/bheuaark/', { hostname: 'localhost' });

const sh = await import('../../../../blocks/sheet/index.js');

describe('Sheets', () => {
it('Test single sheet getData', async () => {
const json = `
{
"total": 3,
"limit": 3,
"offset": 0,
"data": [
{ "Value": "A" },
{ "Value": "B" },
{ "Value": "C" }
],
":type": "sheet"
}`;

const mockFetch = async (url) => {
if (url === 'http://example.com') {
return new Response(json, { status: 200 });
}
return undefined;
};

const savedFetch = window.fetch;
try {
window.fetch = mockFetch;

const sheet = await sh.getData('http://example.com');
expect(sheet.length).to.equal(1);
expect(sheet[0].sheetName).to.equal('data');
expect(sheet[0].data).to.deep.equal([['Value'], ['A'], ['B'], ['C']]);
} finally {
window.fetch = savedFetch;
}
});

it('Test multiple sheet getData', async () => {
const json = `
{
"data": {
"total": 3,
"offset": 0,
"limit": 3,
"data": [
{ "Tag": "red" },
{ "Tag": "blue" },
{ "Tag": "orange" }
]
},
"md": {
"total": 1,
"offset": 0,
"limit": 1,
"data": [{ "Title": "Foo" }]
},
":version": 3,
":names": [
"data",
"md"
],
":type": "multi-sheet"
}`;

const mockFetch = async (url) => {
if (url === 'http://example.com') {
return new Response(json, { status: 200 });
}
return undefined;
};

const savedFetch = window.fetch;
try {
window.fetch = mockFetch;

const sheet = await sh.getData('http://example.com');
expect(sheet.length).to.equal(2);
expect(sheet[0].sheetName).to.equal('data');
expect(sheet[0].data).to.deep.equal([['Tag'], ['red'], ['blue'], ['orange']]);
expect(sheet[1].sheetName).to.equal('md');
expect(sheet[1].data).to.deep.equal([['Title'], ['Foo']]);
} finally {
window.fetch = savedFetch;
}
});
});

0 comments on commit 673f5f4

Please sign in to comment.