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

Fixes bug with v1_2 format #521

Merged
merged 3 commits into from
May 21, 2023
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
41 changes: 34 additions & 7 deletions src/schemas/validateGridFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import fs from 'fs';
import path from 'path';
import { GridFiles } from '.';
import { GridFileV1 } from './GridFileV1';
import { GridFileV1_1 } from './GridFileV1_1';
import { validateGridFile } from './validateGridFile';
import { GridFileV1_2 } from './GridFileV1_2';
import { GridFileV1_1 } from './GridFileV1_1';
import { randomUUID } from 'crypto';
const v = validateGridFile;
const EXAMPLES_DIR = path.join(__dirname, '../../public/examples/');
const exampleGridFiles: GridFiles[] = fs
Expand Down Expand Up @@ -31,6 +33,21 @@ const v1File: GridFileV1 = {
render_dependency: [],
};

const v1_1File: GridFileV1_1 = {
version: '1.1',
cells: [],
columns: [],
rows: [],
borders: [],
cell_dependency: 'foo',
formats: [],
render_dependency: [],
id: randomUUID(),
created: 1,
filename: '1',
modified: 1,
};

describe('validateFile()', () => {
// TODO test the round trip of import to export to import

Expand Down Expand Up @@ -61,9 +78,10 @@ describe('validateFile()', () => {
expect(v(v1FileSansCellDependency)).not.toBe(null);
});

test('Upgrades a valid file from v1 to v1.1', () => {
test('Upgrades a valid file from v1 to v1.2', () => {
const result = v(v1File);
expect(result).toHaveProperty('version', '1.1');
expect(result).not.toBeNull();
expect(result).toHaveProperty('version', '1.2');
expect(result).toHaveProperty('modified');
expect(result).toHaveProperty('created');
expect(result).toHaveProperty('id');
Expand All @@ -79,21 +97,30 @@ describe('validateFile()', () => {
expect(result).not.toHaveProperty('borders[7].horizontal');
});

test('Upgrades a valid file from v1.1 to v1.2', () => {
const result = v(v1_1File);
expect(result).not.toBeNull();
expect(result).toHaveProperty('version', '1.2');
expect(result).toHaveProperty('modified');
expect(result).toHaveProperty('created');
expect(result).toHaveProperty('id');
expect(result).toHaveProperty('filename');
});

test('Returns the file when it matches the most recent schema', () => {
const v1_1File: GridFileV1_1 = {
version: '1.1',
const v1_2File: GridFileV1_2 = {
version: '1.2',
cells: [],
columns: [],
rows: [],
borders: [{ x: 0, y: 0, horizontal: { type: 'line1' } }],
formats: [{ x: 1, y: 1 }],
render_dependency: [],
cell_dependency: 'foo',
modified: 123,
created: 123,
id: '123e4567-e89b-12d3-a456-426614174000',
filename: 'foo',
};
expect(v(v1_1File)).toStrictEqual(v1_1File);
expect(v(v1_2File)).toStrictEqual(v1_2File);
});
});
7 changes: 6 additions & 1 deletion src/schemas/validateGridFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { GridFileSchemaV1 } from './GridFileV1';
import { GridFileSchemaV1_1, upgradeV1toV1_1 } from './GridFileV1_1';
import { GridFile, GridFiles } from '.';
import { debugShowFileIO } from '../debugFlags';
import { GridFileSchemaV1_2, upgradeV1_1toV1_2 } from './GridFileV1_2';

// Ordered by newest first
const files = [{ schema: GridFileSchemaV1_1 }, { schema: GridFileSchemaV1, updateFn: upgradeV1toV1_1 }];
const files = [
{ schema: GridFileSchemaV1_2 },
{ schema: GridFileSchemaV1_1, updateFn: upgradeV1_1toV1_2 },
{ schema: GridFileSchemaV1, updateFn: upgradeV1toV1_1 },
];

/**
* Given arbitrary JSON, validate whether it's a valid file format and return
Expand Down
Loading