Skip to content

Commit

Permalink
test: add tests for theme caching (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
JavadAg authored Sep 19, 2024
1 parent 3dcb1b5 commit 929e533
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 24 deletions.
1 change: 0 additions & 1 deletion cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export default defineConfig({
baseUrl: "http://localhost:3000",
testIsolation: false,
defaultCommandTimeout: 4000,
supportFile: false,
},
});
67 changes: 49 additions & 18 deletions cypress/e2e/tests.cy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference types="cypress" />

import path from "path";
import "cypress-file-upload";

Expand Down Expand Up @@ -184,23 +185,53 @@ describe("Chat Bot Test Suite", () => {
expect(scrollPosition).to.be.closeTo(totalHeight, 5);
});
});
// this test for theme is not neccessary, only checks localstorage store and retrieve
// todo: have a test case that checks cache expiry instead
/* it("Should store and retrieve the theme from localStorage", () => {
const newTheme = {
id: "new-theme",
version: "1.0.0",
baseUrl: "http://example.com",
};
cy.window().then((win) => {
win.localStorage.setItem("theme", JSON.stringify(newTheme));
});
cy.window().then((win) => {
const cachedTheme = JSON.parse(
win.localStorage.getItem("theme") as string

const themeId = "new-theme";
const themeVersion = "1.0.0";
const defaultExpiration = 3;
const themeSettings = { key: "value" };
const inlineStyles = { styleKey: "styleValue" };
const cssStylesText = "body { background-color: red; }";

it("Should retrieve the theme if not expired", () => {
cy.chatbotify.setCachedTheme(
themeId,
themeVersion,
themeSettings,
inlineStyles,
cssStylesText
);

const theme = cy.chatbotify.getCachedTheme(
themeId,
themeVersion,
defaultExpiration
);

expect(theme).to.exist;
expect(theme.settings).to.deep.equal(themeSettings);
expect(theme.inlineStyles).to.deep.equal(inlineStyles);
expect(theme.cssStylesText).to.equal(cssStylesText);
});

it("Should not retrieve the theme if expired", () => {
cy.chatbotify.setCachedTheme(
themeId,
themeVersion,
themeSettings,
inlineStyles,
cssStylesText
);

cy.wait(4000).then(() => {

const theme = cy.chatbotify.getCachedTheme(
themeId,
themeVersion,
defaultExpiration
);
expect(cachedTheme).to.have.property("id", newTheme.id);
expect(cachedTheme).to.have.property("version", newTheme.version);
});
}); */

expect(theme).to.be.null;
})
});
});
1 change: 1 addition & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './functions'
9 changes: 9 additions & 0 deletions cypress/support/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
getCachedTheme,
setCachedTheme,
} from "../../src/services/ThemeService";

cy.chatbotify = {
getCachedTheme,
setCachedTheme,
};
10 changes: 5 additions & 5 deletions src/services/ThemeService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Styles } from "../types/Styles";
import { Theme } from "../types/Theme";
import { ThemeCacheData } from "../types/internal/ThemeCacheData";

const DEFAULT_URL = import.meta.env.VITE_THEME_BASE_CDN_URL;
const DEFAULT_EXPIRATION = import.meta.env.VITE_THEME_DEFAULT_CACHE_EXPIRATION;
const CACHE_KEY_PREFIX = import.meta.env.VITE_THEME_CACHE_KEY_PREFIX;
const DEFAULT_URL = import.meta.env?.VITE_THEME_BASE_CDN_URL;
const DEFAULT_EXPIRATION = import.meta.env?.VITE_THEME_DEFAULT_CACHE_EXPIRATION;
const CACHE_KEY_PREFIX = import.meta.env?.VITE_THEME_CACHE_KEY_PREFIX;

/**
* Fetches the cached theme if it exist and checks for expiry.
Expand All @@ -14,7 +14,7 @@ const CACHE_KEY_PREFIX = import.meta.env.VITE_THEME_CACHE_KEY_PREFIX;
* @param version version of the theme
* @param cacheDuration duration that the theme should be cached for
*/
const getCachedTheme = (id: string, version: string, cacheDuration: number): ThemeCacheData | null => {
export const getCachedTheme = (id: string, version: string, cacheDuration: number): ThemeCacheData | null => {
const cachedTheme = localStorage.getItem(`${CACHE_KEY_PREFIX}_${id}_${version}`);

// if unable to find theme, not cached so return null
Expand Down Expand Up @@ -46,7 +46,7 @@ const getCachedTheme = (id: string, version: string, cacheDuration: number): The
* @param inlineStyles inline styles to cache
* @param cssStylesText css styles to cache
*/
const setCachedTheme = (id: string, version: string, settings: Settings, inlineStyles: Styles,
export const setCachedTheme = (id: string, version: string, settings: Settings, inlineStyles: Styles,
cssStylesText: string) => {

const milliseconds = new Date().getTime();
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": [
"cypress/**/*",
"src/**/*",
"types/*"
],
Expand Down
9 changes: 9 additions & 0 deletions types/cypress.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare namespace Cypress {
interface Chainable<Subject = any> {
chatbotify: {
getCachedTheme(id: string, version: string, cacheDuration: number): ThemeCacheData | null
setCachedTheme(id: string, version: string, settings: Settings, inlineStyles: Styles, cssStylesText: string)
}

}
}

0 comments on commit 929e533

Please sign in to comment.