Skip to content

Commit

Permalink
Added client propagaion with getInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
juanhapes committed Jul 18, 2019
1 parent 9e8905b commit ced2b8e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.2.0] - 2019-07-18
### Added
- `getInstance` with client propagation

## [1.1.1] - 2019-07-15
### Changed
- `model` package instead of `model-controller`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/janis-commerce/active-client.svg?branch=master)](https://travis-ci.org/janis-commerce/active-client)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/active-client/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/active-client?branch=master)

**ActiveClient** is a simple module for getting a client from a field and a value.
**ActiveClient** is a simple module for getting an Active Client from a field and a value.
The module only has one method and a Model for the get.

## Installation
Expand Down
12 changes: 12 additions & 0 deletions lib/active-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ class ActiveClient {

const client = clients && clients.length === 1 ? clients[0] : false;

if(client)
client.getInstance = this.getInstance(client);

return client;
}

static getInstance(client) {
return TheClass => {
const instance = new TheClass();
instance.client = client;

return instance;
};
}

}

module.exports = ActiveClient;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@janiscommerce/active-client",
"version": "1.1.1",
"description": "",
"version": "1.2.0",
"description": "A simple module for getting an Active Client from a field and a value",
"main": "index.js",
"scripts": {
"test": "export TEST_ENV=true; mocha --exit -R nyan --recursive tests/",
Expand Down
18 changes: 18 additions & 0 deletions tests/active-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ describe('ActiveClient', () => {
foo: 'bar'
};

class RandomClass {

}

afterEach(() => {
sandbox.restore();
});
Expand Down Expand Up @@ -48,4 +52,18 @@ describe('ActiveClient', () => {
assert.deepEqual(client, { ...theClient });
});

it('should propagate client when getting an instance from a client', async () => {

sandbox.stub(ModelClient.prototype, 'get')
.returns([theClient]);

const client = await ActiveClient.getByField('foo', 'bar');

assert.equal(typeof client.getInstance, 'function');

const instance = client.getInstance(RandomClass);

assert.deepEqual(client, instance.client);
});

});

0 comments on commit ced2b8e

Please sign in to comment.