This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bdd546
commit e27f200
Showing
31 changed files
with
664 additions
and
772 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,7 @@ module.exports = { | |
jest: true | ||
} | ||
} | ||
] | ||
], | ||
|
||
extends: ["plugin:vue/essential", "@vue/prettier"] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
src/components/SideNav/Model/__tests__/normalize-items.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import normalizeItems from "./../normalize-items"; | ||
import { normalizeItem } from "./../normalize-items"; | ||
|
||
// foo is a mock function | ||
describe("item normalizer", () => { | ||
describe("normalizeItem", () => { | ||
it("works with {}", () => { | ||
const normalized = normalizeItem({}); | ||
expect(normalized).toHaveProperty("$_fdId"); | ||
expect(normalized).toHaveProperty("$_fdChildren", []); | ||
}); | ||
it("works with null", () => { | ||
const normalized = normalizeItem(null); | ||
expect(normalized).toHaveProperty("$_fdId"); | ||
expect(normalized).toHaveProperty("$_fdChildren", []); | ||
}); | ||
it("works with undefined", () => { | ||
const normalized = normalizeItem(undefined); | ||
expect(normalized).toHaveProperty("$_fdId"); | ||
expect(normalized).toHaveProperty("$_fdChildren", []); | ||
}); | ||
|
||
it("works with simple object", () => { | ||
const normalized = normalizeItem({ firstName: "chris" }); | ||
expect(normalized).toHaveProperty("$_fdId"); | ||
expect(normalized).toHaveProperty("$_fdChildren", []); | ||
expect(normalized).toHaveProperty("firstName", "chris"); | ||
}); | ||
|
||
it("re-uses id", () => { | ||
const normalized = normalizeItem({ id: "123" }); | ||
expect(normalized).toHaveProperty("$_fdId", "123"); | ||
expect(normalized).toHaveProperty("id", "123"); | ||
expect(normalized).toHaveProperty("$_fdChildren", []); | ||
}); | ||
|
||
it("re-uses children", () => { | ||
const normalized = normalizeItem({ | ||
children: [{ firstName: "chris" }] | ||
}); | ||
expect(normalized).toHaveProperty("$_fdId"); | ||
const children = normalized["$_fdChildren"]; | ||
expect(children).toMatchObject([{ firstName: "chris" }]); | ||
}); | ||
}); | ||
describe("normalizeItems", () => { | ||
it("works with empty array", () => { | ||
const normalized = normalizeItems([]); | ||
expect(normalized).toEqual([]); | ||
}); | ||
|
||
it("works with array with single object", () => { | ||
const [normalized] = normalizeItems([{}]); | ||
expect(normalized).toHaveProperty("id"); | ||
}); | ||
|
||
it("works with array with children", () => { | ||
const [normalized] = normalizeItems([ | ||
{ | ||
fn: "chris", | ||
children: [ | ||
{ | ||
fn: "andi" | ||
} | ||
] | ||
} | ||
]); | ||
expect(normalized).toHaveProperty("fn"); | ||
expect(normalized).toHaveProperty("id"); | ||
expect(normalized).toHaveProperty("children"); | ||
const { children } = normalized; | ||
const [child] = children; | ||
expect(child).toHaveProperty("fn"); | ||
expect(child).toHaveProperty("id"); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// @ts-check | ||
import { shortUuid } from "./../../../lib"; | ||
|
||
const $_FDID = "$_fdId"; | ||
const $_FDCHILDREN = "$_fdChildren"; | ||
|
||
/** | ||
* @typedef {object} NormalizedItem | ||
* @prop {string} $_fdId; | ||
* @prop {NormalizedItem[]} $_fdChildren; | ||
*/ | ||
|
||
/** | ||
* @param {NormalizedItem} item | ||
* @returns {string} | ||
*/ | ||
export const normalizedId = item => item[$_FDID]; | ||
/** | ||
* @param {NormalizedItem} item | ||
* @returns {NormalizedItem[]} | ||
*/ | ||
export const normalizedChildren = item => item[$_FDCHILDREN]; | ||
|
||
/** | ||
* @param {any} raw | ||
* @returns {NormalizedItem} | ||
*/ | ||
export function normalizeItem(raw) { | ||
if (raw == null) { | ||
return normalizeItem({}); | ||
} | ||
const id = raw.id || shortUuid(); | ||
const children = normalizeItems(raw.children || []); | ||
return { | ||
...raw, | ||
id, | ||
children, | ||
[$_FDID]: id, | ||
[$_FDCHILDREN]: children | ||
}; | ||
} | ||
|
||
function normalizeItems(items) { | ||
return items.map(normalizeItem); | ||
} | ||
|
||
export default normalizeItems; |
Oops, something went wrong.