-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(content): add caching layer to content (#18)
* feat(content): add caching layer to content * test(routing.global): remove only * test(routing.global): small fixes
- Loading branch information
Showing
10 changed files
with
214 additions
and
36 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 |
---|---|---|
@@ -1,6 +1,23 @@ | ||
import { Page } from "fsxa-api"; | ||
|
||
export function useContent() { | ||
const content = useState<Page | null>(); | ||
return { content }; | ||
const currentPage = useState<Page | null>("currentPage"); | ||
const cachedPages = useState<{ | ||
[caasId: string]: Page; | ||
}>("cachedPages", () => ({})); | ||
|
||
function findCachedPageByCaaSId(caasDocumentId: string) { | ||
return cachedPages.value[caasDocumentId]; | ||
} | ||
|
||
function addToCache(key: string, data: Page) { | ||
if (!cachedPages.value[key]) cachedPages.value[key] = data; | ||
} | ||
|
||
return { | ||
currentPage, | ||
cachedPages, | ||
addToCache, | ||
findCachedPageByCaaSId, | ||
}; | ||
} |
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 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 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,25 @@ | ||
const baseURL = Cypress.env("cyBaseURL"); | ||
|
||
describe(`slug page`, () => { | ||
it("navigate to page => render content", () => { | ||
cy.visit(`${baseURL}/Unsere-Lösungen`); | ||
cy.get("body").should("contain", "Unsere Lösungen"); | ||
}); | ||
|
||
it("navigate back and forth => don't make new network calls for cached content", () => { | ||
cy.intercept({ | ||
method: "GET", | ||
url: "/api/elements", | ||
}).as("fetchContent"); | ||
cy.visit(`${baseURL}`); | ||
cy.get('[href="/Unsere-Lösungen/"]').click(); | ||
cy.get('[href="/Startseite/"]').click(); | ||
cy.get('[href="/Unsere-Lösungen/"]').click(); | ||
cy.wait(3000); | ||
|
||
// newtwork calls happen once on the server | ||
cy.get("@fetchContent").should("eq", null); | ||
}); | ||
}); | ||
|
||
export {}; |
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 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 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,81 @@ | ||
import { it, expect, describe, beforeEach } from "vitest"; | ||
import { useContent } from "../../composables/content"; | ||
import { createPage } from "../testutils/createPage"; | ||
import { clearMockedState } from "../testutils/nuxtMocks"; | ||
|
||
describe("useContent", () => { | ||
it("useContent => provide default current page, cached pages", () => { | ||
const { currentPage, cachedPages } = useContent(); | ||
expect(currentPage.value).toBeUndefined(); | ||
expect(cachedPages.value).toEqual({}); | ||
}); | ||
|
||
describe("findCachedPageByCaaSId", () => { | ||
beforeEach(() => { | ||
clearMockedState(); | ||
}); | ||
it("item does not exist in cached pages => return undefined", () => { | ||
const page1 = createPage(); | ||
const page2 = createPage(); | ||
const page3 = createPage(); | ||
|
||
const { cachedPages, findCachedPageByCaaSId } = useContent(); | ||
|
||
cachedPages.value[page1.refId] = page1; | ||
cachedPages.value[page2.refId] = page2; | ||
cachedPages.value[page3.refId] = page3; | ||
|
||
expect(findCachedPageByCaaSId("unknown-id")).toBeUndefined(); | ||
}); | ||
it("item exists => return item", () => { | ||
const page1 = createPage(); | ||
const page2 = createPage(); | ||
const page3 = createPage(); | ||
|
||
const { cachedPages, findCachedPageByCaaSId } = useContent(); | ||
|
||
cachedPages.value[page1.refId] = page1; | ||
cachedPages.value[page2.refId] = page2; | ||
cachedPages.value[page3.refId] = page3; | ||
|
||
expect(findCachedPageByCaaSId(page2.refId)).toBe(page2); | ||
}); | ||
}); | ||
describe("addToCache", () => { | ||
beforeEach(() => { | ||
clearMockedState(); | ||
}); | ||
it("item does not exist => add new item", () => { | ||
const page1 = createPage(); | ||
const page2 = createPage(); | ||
const page3 = createPage(); | ||
|
||
const { cachedPages, addToCache } = useContent(); | ||
|
||
addToCache(page1.refId, page1); | ||
addToCache(page2.refId, page2); | ||
addToCache(page3.refId, page3); | ||
|
||
expect(cachedPages.value[page1.refId]).toBe(page1); | ||
expect(cachedPages.value[page2.refId]).toBe(page2); | ||
expect(cachedPages.value[page3.refId]).toBe(page3); | ||
|
||
expect(Object.keys(cachedPages.value).length).toBe(3); | ||
}); | ||
it("item exists => don't add new item", () => { | ||
const page1 = createPage(); | ||
const page2 = createPage(); | ||
|
||
const { cachedPages, addToCache } = useContent(); | ||
|
||
addToCache(page1.refId, page1); | ||
addToCache(page2.refId, page2); | ||
addToCache(page2.refId, page2); | ||
|
||
expect(cachedPages.value[page1.refId]).toBe(page1); | ||
expect(cachedPages.value[page2.refId]).toBe(page2); | ||
|
||
expect(Object.keys(cachedPages.value).length).toBe(2); | ||
}); | ||
}); | ||
}); |
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 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 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,22 @@ | ||
import { NavigationItem } from "fsxa-api"; | ||
import { faker } from "@faker-js/faker"; | ||
|
||
export function createNavigationItem( | ||
optionalNavigationItem?: Partial<NavigationItem> | ||
): NavigationItem { | ||
const NavigationItem: NavigationItem = { | ||
caasDocumentId: faker.datatype.uuid(), | ||
id: faker.datatype.uuid(), | ||
contentReference: faker.internet.url(), | ||
customData: {}, | ||
seoRoute: faker.random.word(), | ||
seoRouteRegex: null, | ||
label: faker.random.word(), | ||
permissions: { | ||
allowed: [], | ||
denied: [], | ||
}, | ||
parentIds: [], | ||
}; | ||
return { ...NavigationItem, ...optionalNavigationItem }; | ||
} |