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

fix : permissable passing of date for generic versioned storage #1736

Merged
merged 2 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/token-transformer",
"internalConsoleOptions": "neverOpen"
},
{
"name": "Test current file",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest",
"args": [
"--testPathPattern=${fileBasenameNoExtension}",
"--no-cache",
"--coverage=false"
],
"cwd": "${workspaceFolder}",
"sourceMaps": true,
"console": "integratedTerminal"
}
]
}
15 changes: 7 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"set-value": "^3.0.2",
"storyblok-js-client": "^3.3.1",
"use-debounce": "^6.0.1",
"zod": "^3.14.4"
"zod": "^3.21.4"
},
"devDependencies": {
"@babel/core": "^7.12.16",
Expand Down
4 changes: 2 additions & 2 deletions src/storage/GenericVersionedStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const genericVersionedSchema = singleFileSchema.extend({
values: z.record(tokensMapSchema),
version: z.string().optional(),
$themes: z.array(themeObjectSchema).optional(),
updatedAt: z.number().optional(),
updatedAt: z.coerce.date().optional(),
});

type GenericVersionedMeta = {
Expand Down Expand Up @@ -139,9 +139,9 @@ export class GenericVersionedStorage extends RemoteTokenStorage<GenericVersioned
if (response.ok) {
const parsedJsonData = await response.json();
const validationResult = await genericVersionedSchema.safeParseAsync(parsedJsonData);

if (validationResult.success) {
const GenericVersionedData = validationResult.data as unknown as GenericVersionedData;
GenericVersionedData.updatedAt = (GenericVersionedData.updatedAt as unknown as Date).toISOString();
return this.convertGenericVersionedDataToFiles(GenericVersionedData);
}
}
Expand Down
72 changes: 70 additions & 2 deletions src/storage/__tests__/GenericVersionedStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ describe('GenericVersionedStorage', () => {
});

it('can read GenericVersioned data', async () => {
const unixTime = 1666785400000;
const date = new Date(unixTime);
mockFetch.mockImplementationOnce(() => (
Promise.resolve({
ok: true,
json: () => Promise.resolve({
version: '1',
updatedAt: 1666785400000,
updatedAt: unixTime,
values: {
global: {
colors: {
Expand Down Expand Up @@ -96,7 +98,73 @@ describe('GenericVersionedStorage', () => {
path: '$metadata.json',
data: {
version: '1',
updatedAt: 1666785400000,
updatedAt: date.toISOString(),
},
});
expect(result[2]).toEqual({
type: 'tokenSet',
path: 'global.json',
name: 'global',
data: {
colors: {
red: {
type: TokenTypes.COLOR,
value: '#ff0000',
},
},
},
});
});

it('can parse date as an iso string', async () => {
const date = new Date(1666785400000);
mockFetch.mockImplementationOnce(() => (
Promise.resolve({
ok: true,
json: () => Promise.resolve({
version: '1',
updatedAt: date.toISOString(),
values: {
global: {
colors: {
red: {
type: TokenTypes.COLOR,
value: '#ff0000',
},
},
},
},
$themes: [
{
id: 'light',
name: 'Light',
selectedTokenSets: {},
},
],
}),
})
));

const storage = new GenericVersionedStorage(url, GenericVersionedStorageFlow.READ_WRITE_CREATE, defaultHeaders);
const result = await storage.read();

expect(result[0]).toEqual({
type: 'themes',
path: '$themes.json',
data: [
{
id: 'light',
name: 'Light',
selectedTokenSets: {},
},
],
});
expect(result[1]).toEqual({
type: 'metadata',
path: '$metadata.json',
data: {
version: '1',
updatedAt: date.toISOString(),
},
});
expect(result[2]).toEqual({
Expand Down