Skip to content

Commit

Permalink
chore: updated test-util functions deps and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
avil13 committed Jul 16, 2024
1 parent 0827059 commit 2cf2ddf
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 76 deletions.
59 changes: 37 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Vue-Act-Master
# Act-Master

A way to separate business logic from application view.

The easiest library to create a flexible application architecture.

![npm bundle size](https://img.shields.io/bundlephobia/minzip/vue-act-master)
![npm version](https://img.shields.io/npm/v/vue-act-master)

![npm bundle size](https://img.shields.io/bundlephobia/minzip/act-master)
![npm version](https://img.shields.io/npm/v/act-master)

## To work with Vue, there are now even fewer dependencies. Just use `act-mater`.


<div align="center">
<img src="https://raw.githubusercontent.com/avil13/vue-act-master/master/assets/act-master-logo.svg" alt="vue-act-master">
Expand All @@ -25,34 +29,32 @@ The easiest library to create a flexible application architecture.
## Installation

```bash
npm install vue-act-master
npm install act-master
```

# Usage

```ts
// main.ts
// install vue-act-master plugin
import Vue from 'vue';
import App from './App.vue';

import { VueActMaster } from 'vue-act-master';
import { act } from 'act-master';
import { VueActMaster } from 'act-master/vue';
import { createApp } from 'vue';

// Actions array
import { actions } from '../act/actions';
import { actions } from '@/act/actions';

Vue.use(VueActMaster, {
act.init({
actions,
});

new Vue({
el: '#app',
render: (h) => h(App),
});
VueActMaster.setActMaster(act());

const app = createApp(App);

app.use(VueActMaster); // Installation
```

```ts
// ../act/actions
// @/act/actions
export const actions: ActMasterAction[] = [
new GetDataAction(),
];
Expand All @@ -77,24 +79,37 @@ The action is now available to you in components and you can easily highlight th

This will help you test components and change the API more easily.


```html
// App.vue

<script>
import { act } from 'act-master'
export default {
data() {
return {
myData: null,
myData1: null,
myData2: null,
};
},
async mounted() {
console.log(this.myData); // null
console.log(this.myData1, this.myData2); // null, null
// Subscribe
act().on('GetData', (data) => {
this.myData2 = data;
});
this.myData = await this.$act.exec('GetData');
this.myData1 = await this.$act.exec('GetData');
console.log(this.myData);
console.log(this.myData1, this.myData2);
// {
// "userId": 1,
// "id": 1,
// "title": "delectus aut autem",
// "completed": false
// },
// {
// "userId": 1,
// "id": 1,
Expand Down
104 changes: 84 additions & 20 deletions packages/act-master/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ A way to separate business logic from application view.

The easiest library to create a flexible application architecture.


![npm bundle size](https://img.shields.io/bundlephobia/minzip/act-master)
![npm version](https://img.shields.io/npm/v/act-master)

## To work with Vue, there are now even fewer dependencies. Just use `act-mater`.


<div align="center">
<img src="https://raw.githubusercontent.com/avil13/vue-act-master/master/assets/act-master-logo.svg" alt="vue-act-master">
</div>

---
## 📗 [Documentation](https://avil13.github.io/vue-act-master/)


## 🗺 [Example project structure](https://github.com/avil13/vue-act-master/blob/master/packages/example/README.md)
## 📗 [Documentation](https://avil13.github.io/vue-act-master/)

## 🧪 [Test writing with "ActTest"](https://github.com/avil13/vue-act-master/blob/master/packages/act-master/src/test-utils/README.md)


---

# Example
Expand All @@ -32,27 +35,88 @@ npm install act-master
# Usage

```ts
import { ActMaster } from 'act-master';
// main.ts
import { act } from 'act-master';
import { VueActMaster } from 'act-master/vue';
import { createApp } from 'vue';

const $act = new ActMaster();
import { actions } from '@/act/actions';

const action = {
name: 'LogData',
exec(message) {
console.log('Log:', message);
return 'Success!!!';
}
};
act.init({
actions,
});

$act.addAction(action);
VueActMaster.setActMaster(act());

// Use action
const result = await $act.exec('LogData', 'Hello world');
const app = createApp(App);

console.log('Result:', result);
app.use(VueActMaster); // Installation
```
console.log
```bash
Log: Hello world
Result: Success!!!

```ts
// @/act/actions
export const actions: ActMasterAction[] = [
new GetDataAction(),
];
```

```ts
// action-get-data.ts
import { ActMasterAction } from 'vue-act-master';

export class GetDataAction implements ActMasterAction {
name = 'GetData';

async exec() {
const url = 'https://jsonplaceholder.typicode.com/todos/1';

const response = await fetch(url);
return response.json();
}
}
```
The action is now available to you in components and you can easily highlight the business logic.

This will help you test components and change the API more easily.

```html
// App.vue

<script>
import { act } from 'act-master'
export default {
data() {
return {
myData1: null,
myData2: null,
};
},
async mounted() {
console.log(this.myData1, this.myData2); // null, null
// Subscribe
act().on('GetData', (data) => {
this.myData2 = data;
});
this.myData1 = await this.$act.exec('GetData');
console.log(this.myData1, this.myData2);
// {
// "userId": 1,
// "id": 1,
// "title": "delectus aut autem",
// "completed": false
// },
// {
// "userId": 1,
// "id": 1,
// "title": "delectus aut autem",
// "completed": false
// }
}
}
</script>
```
13 changes: 7 additions & 6 deletions packages/act-master/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "act-master",
"version": "2.5.0",
"version": "2.5.1",
"author": "avil13",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down Expand Up @@ -55,12 +55,13 @@
"url": "https://github.com/avil13/vue-act-master.git"
},
"devDependencies": {
"typescript": "^5.4.5",
"vite": "^5.2.10",
"vitest": "^1.5.2",
"vue": "^3.4.25"
"@babel/types": "^7.24.9",
"typescript": "^5.5.3",
"vite": "^5.3.4",
"vitest": "^2.0.3",
"vue": "^3.4.31"
},
"dependencies": {
"@vue/devtools-api": "^6.6.1"
"@vue/devtools-api": "^6.6.3"
}
}
19 changes: 0 additions & 19 deletions packages/act-master/src/__tests__/act-master-constructor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,8 @@ describe('ActMaster constructor options', () => {
expect(ActTest.entityCount('di')).toBe(3);
});

it('errorOnReplaceAction:[true]', () => {
const actions = [{ name: 'test-name', exec }];

init({
actions,
});

const add = () => $act.addActions(actions);

expect(add).toThrow('Action "test-name" already existing');
});

it('errorOnEmptyAction:[true]', async () => {
init({});

expect(() => $act.exec('test')).rejects.toThrow();
});

it('errorOnReplaceDI', () => {
init({
errorOnReplaceDI: true,
di: {
api: {},
},
Expand Down
8 changes: 1 addition & 7 deletions packages/act-master/src/act-master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class ActMaster implements IActMaster {
>();

private readonly config: devActMasterConfig = {
errorOnReplaceDI: false,
autoUnsubscribeCallback: undefined,
errorHandlerEventName: undefined,
};
Expand All @@ -74,7 +73,6 @@ export class ActMaster implements IActMaster {
const {
actions,
di,
errorOnReplaceDI,
autoUnsubscribeCallback,
errorHandlerEventName,
} = options;
Expand All @@ -98,10 +96,6 @@ export class ActMaster implements IActMaster {
}
}

if (typeof errorOnReplaceDI === 'boolean') {
this.config.errorOnReplaceDI = errorOnReplaceDI;
}

if (typeof errorHandlerEventName === 'string') {
this.config.errorHandlerEventName = errorHandlerEventName;
}
Expand Down Expand Up @@ -454,7 +448,7 @@ export class ActMaster implements IActMaster {
}

setDI(key: string, ctx: any): ActMaster {
if (this.config.errorOnReplaceDI && this._DIContainer[key]) {
if (this._DIContainer[key]) {
throw new KeyAlreadyExistsInDIError(key);
}
this._DIContainer[key] = ctx;
Expand Down
4 changes: 4 additions & 0 deletions packages/act-master/src/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class ActTest {
}

static addActions(actions: ActMasterAction[]): void {
if (!ActTest.$act) {
ActTest.getInstance();
}

ActTest.$act.addActions(actions);
}

Expand Down
2 changes: 0 additions & 2 deletions packages/act-master/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ export interface IActMaster {
export interface ActMasterOptions {
actions?: ActMasterAction[];
di?: DIMap;
errorOnReplaceDI?: boolean;
// method for calling auto unsubscribe
autoUnsubscribeCallback?: (args: autoUnsubscribeArgs) => void;
errorHandlerEventName?: ActEventName;
}

export type devActMasterConfig = {
errorOnReplaceDI: ActMasterOptions['errorOnReplaceDI'];
autoUnsubscribeCallback: ActMasterOptions['autoUnsubscribeCallback'];
errorHandlerEventName?: ActEventName;
};
Expand Down

0 comments on commit 2cf2ddf

Please sign in to comment.