Skip to content

Commit

Permalink
migrate localisation platform middleware into bootstrap saga
Browse files Browse the repository at this point in the history
  • Loading branch information
jancajthaml authored Aug 9, 2024
1 parent 86c5eda commit 4136d9b
Show file tree
Hide file tree
Showing 27 changed files with 764 additions and 542 deletions.
23 changes: 13 additions & 10 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish to npm
name: Publish to registry.npmjs.org

on:
push:
Expand All @@ -7,8 +7,9 @@ on:

jobs:

build:
name: Publish to npm
publish:
needs: test
name: Publish to registry.npmjs.org
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -17,13 +18,15 @@ jobs:
node-version: 20.11.0
registry-url: https://registry.npmjs.org
scope: "@lastui"
- run: npm ci --no-fund --no-audit
- run: npm run build:dependencies
- run: npm run build:platform
- run: npm run build:bootstrap
- run: npm run postbuild
- run: node ./cli/index.js --cwd=platform test
- run: node ./cli/index.js --cwd=bootstrap test
- name: Restore cached workspace
id: cache
uses: actions/cache/restore@v4
with:
path: |
dependencies/dll
platform/dll
bootstrap/dll
key: cache-${{ github.sha }}
- name: Publish @lastui/dependencies
continue-on-error: true
working-directory: dependencies
Expand Down
18 changes: 12 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ on:

jobs:

build:
name: Run Tests and Report Coverage
test:
name: Compile and Test
permissions:
checks: write
pull-requests: write
Expand All @@ -25,10 +25,16 @@ jobs:
scope: "@lastui"
- run: npm ci --no-fund --no-audit
- run: npm run lint -- --debug
- run: npm run build:dependencies
- run: npm run build:platform
- run: npm run build:bootstrap
- run: npm run postbuild
- run: npm run build
- name: Cache workspace
id: cache
uses: actions/cache/save@v4
with:
path: |
dependencies/dll
platform/dll
bootstrap/dll
key: cache-${{ github.sha }}
- name: Test package platform
working-directory: platform
run: node ../cli/index.js test
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/src/component/__tests__/Entrypoint.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const initialState = {
runtime: {
entrypoint: null,
},
env: {},
localisation: {},
};

const mockStore = configureStore([]);
Expand Down
14 changes: 7 additions & 7 deletions bootstrap/src/component/__tests__/Globalisation.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { withRedux } from "@lastui/rocker/test";
import Globalisation from "../Globalisation";

const initialState = {
env: {
localisation: {
language: "en-US",
messages: {},
},
Expand Down Expand Up @@ -39,8 +39,8 @@ describe("<Globalisation />", () => {
it("is supported", () => {
const store = mockStore({
...initialState,
env: {
...initialState.env,
localisation: {
...initialState.localisation,
messages: {
"en-US": {
existant: "message with key {key} and value {value}",
Expand Down Expand Up @@ -85,8 +85,8 @@ describe("<Globalisation />", () => {
runtime: {
entrypoint: "entrypoint",
},
env: {
...initialState.env,
localisation: {
...initialState.localisation,
language: "boo",
},
});
Expand All @@ -110,8 +110,8 @@ describe("<Globalisation />", () => {
runtime: {
entrypoint: "entrypoint",
},
env: {
...initialState.env,
localisation: {
...initialState.localisation,
messages: {
"en-US": {
existant: "message with key {key and value {value}",
Expand Down
10 changes: 5 additions & 5 deletions bootstrap/src/component/__tests__/Main.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const initialState = {
entrypoint: "some-entrypoint",
initialized: false,
},
env: {
localisation: {
language: null,
messages: {},
},
Expand All @@ -26,17 +26,17 @@ const mockStore = configureStore([])((actions) => {
...state.runtime,
initialized: true,
},
env: {
...state.env,
localisation: {
...state.localisation,
},
};
} else if (action.type === constants.SET_LANGUAGE) {
state = {
runtime: {
...state.runtime,
},
env: {
...state.env,
localisation: {
...state.localisation,
language: action.payload.language,
},
};
Expand Down
2 changes: 2 additions & 0 deletions bootstrap/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const I18N_ADD_MESSAGES = "@@bootstrap/I18N_ADD_MESSAGES";
export const I18N_REMOVE_MESSAGES = "@@bootstrap/I18N_REMOVE_MESSAGES";
205 changes: 205 additions & 0 deletions bootstrap/src/reducer/__tests__/localisation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import * as constants from "../../constants";
import reducer, { initialState } from "../localisation";

describe("localisation reducer", () => {
describe("is reducer", () => {
it("has initial state", () => {
const action = {
type: "non-handled",
};
expect(reducer(undefined, action)).toEqual(initialState);
});
it("has default case", () => {
const action = {
type: "non-handled",
};
expect(reducer(initialState, action)).toEqual(initialState);
});
});

describe("I18N_REMOVE_MESSAGES", () => {
it("purges local shared state of module", () => {
const action = {
type: constants.I18N_REMOVE_MESSAGES,
payload: {
module: "my-feature",
},
};
const state = {
...initialState,
local: {
"my-feature": {
hair: "yes",
},
},
};
const expectedState = {
...initialState,
};

expect(reducer(state, action)).toEqual(expectedState);
});

it("purges localisation messages owned by this module", () => {
const state = {
...initialState,
language: "en-US",
};

const addMessagesAction = {
type: constants.I18N_ADD_MESSAGES,
payload: {
language: "en-US",
batch: [
{
module: "my-feature",
data: {
"message.key": "message.value",
},
},
],
},
};

const action = {
type: constants.I18N_REMOVE_MESSAGES,
payload: {
module: "my-feature",
},
};
const nextState = reducer(state, addMessagesAction);
expect(nextState.messages["en-US"]["message.key"]).toEqual("message.value");
const expectedState = {
...state,
messages: {
"en-US": {},
},
};
expect(reducer(nextState, action)).toEqual(expectedState);
});
});

describe("I18N_ADD_MESSAGES", () => {
it("empty batch", () => {
const state = {
...initialState,
language: "en-US",
};

const action = {
type: constants.I18N_ADD_MESSAGES,
payload: {
batch: [],
language: "en-US",
},
};
expect(reducer(state, action)).toEqual(state);
});

it("empty batch different language", () => {
const action = {
type: constants.I18N_ADD_MESSAGES,
payload: {
batch: [],
language: "fr-FR",
},
};

const expectedState = {
...initialState,
language: "fr-FR",
};

expect(reducer(initialState, action)).toEqual(expectedState);
});

it("non empty batch same language", () => {
const action = {
type: constants.I18N_ADD_MESSAGES,
payload: {
batch: [
{
module: "my-feature",
data: {
existing: "new",
"message.key": "message.value",
nested: {
localisation: "value",
},
},
},
],
language: "en-US",
},
};
const state = {
...initialState,
language: "en-US",
messages: {
"en-US": {
existing: "old",
},
},
};
const expectedState = {
...initialState,
language: "en-US",
messages: {
"en-US": {
existing: "new",
"message.key": "message.value",
"nested.localisation": "value",
},
},
};

expect(reducer(state, action)).toEqual(expectedState);
});

it("non empty batch different language", () => {
const action = {
type: constants.I18N_ADD_MESSAGES,
payload: {
batch: [
{
module: "my-feature",
data: {
existing: "new",
"message.key": "message.value",
nested: {
localisation: "value",
},
},
},
],
language: "fr-FR",
},
};
const state = {
...initialState,
language: "en-US",
messages: {
"en-US": {
existing: "old",
},
},
};
const expectedState = {
...initialState,
language: "fr-FR",
messages: {
"en-US": {
existing: "old",
},
"fr-FR": {
existing: "new",
"message.key": "message.value",
"nested.localisation": "value",
},
},
};

expect(reducer(state, action)).toEqual(expectedState);
});
});
});
3 changes: 2 additions & 1 deletion bootstrap/src/reducer/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* istanbul ignore file */
import localisationReducer from "./localisation";
import runtimeReducer from "./runtime";

export { runtimeReducer };
export { runtimeReducer, localisationReducer };
Loading

0 comments on commit 4136d9b

Please sign in to comment.