Skip to content

Commit

Permalink
fix: rewrite tests and fix review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Hunter Achieng <[email protected]>
  • Loading branch information
hunterachieng committed Oct 16, 2024
1 parent de0e0b2 commit 0769315
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 97 deletions.
12 changes: 6 additions & 6 deletions packages/mojatax/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"tags": [
{
"title": "example",
"description": "post(\"patient\", { \"name\":\"Bukayo\" });",
"caption": "Make a POST request create an invoice"
"description": "post(\"CreateInvoice\", {\ninvoice_id: 'PID092',\ncustomerId: '102',\nitems: [],\n});",
"caption": "Make a POST request to create an invoice"
},
{
"title": "function",
Expand Down Expand Up @@ -75,7 +75,7 @@
},
{
"title": "state",
"description": "{HttpState}"
"description": "{MojataxHttpState}"
}
]
},
Expand All @@ -95,8 +95,8 @@
"tags": [
{
"title": "example",
"description": "request(\"POST\", \"patient\", { \"name\":\"Bukayo\" });",
"caption": "Make a POST request create an invoice"
"description": "request(\"POST\", \"/client/CreateInvoice\", {\ninvoice_id: 'PID092',\ncustomerId: '102',\nitems: [],\n});",
"caption": "Make a POST request to create an invoice"
},
{
"title": "function",
Expand Down Expand Up @@ -166,7 +166,7 @@
},
{
"title": "state",
"description": "{HttpState}"
"description": "{MojataxHttpState}"
}
]
},
Expand Down
42 changes: 2 additions & 40 deletions packages/mojatax/src/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,7 @@ export function execute(...operations) {
* post("CreateInvoice", {
* invoice_id: 'PID092',
* customerId: '102',
* customerIdType: '6',
* customerName: 'MojaONe LTD',
* mobileNumber: '0787640622',
* items: [
* {
* id: 1,
* desc: 'Solar Panel',
* qty: 3,
* taxcode: 1,
* amt: '5000.00',
* },
* ],
* totals: {
* totaltaxincl: '15000.00',
* discount: '0',
* },
* payments: {
* pmttype: 'INVOICE',
* pmtamount: '15000.00',
* },
* items: [],
* });
* @function
* @public
Expand All @@ -98,26 +79,7 @@ export function post(path, data, options, callback) {
* request("POST", "/client/CreateInvoice", {
* invoice_id: 'PID092',
* customerId: '102',
* customerIdType: '6',
* customerName: 'MojaONe LTD',
* mobileNumber: '0787640622',
* items: [
* {
* id: 1,
* desc: 'Solar Panel',
* qty: 3,
* taxcode: 1,
* amt: '5000.00',
* },
* ],
* totals: {
* totaltaxincl: '15000.00',
* discount: '0',
* },
* payments: {
* pmttype: 'INVOICE',
* pmtamount: '15000.00',
* },
* items: [],
* });
* @function
* @public
Expand Down
17 changes: 9 additions & 8 deletions packages/mojatax/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ import {
import nodepath from 'node:path';

export const authorize = state => {
const auth = state.configuration;
const auth = state.configuration ?? {};
if (auth.access_token) {
return state;
}
const clientId = auth.clientId;
const password = auth.password;

const { clientId: client_id, password } = auth;

const headers = {};

if (clientId && password) {
if (client_id && password) {
Object.assign(headers, {
'Content-Type': 'application/json',
});

const body = {
client_id: clientId,
password: password,
client_id,
password,
};
const options = {
body: JSON.stringify(body),
body,
headers,
method: 'POST',
parseAs: 'json',
Expand Down Expand Up @@ -62,7 +63,7 @@ export const prepareNextState = (state, response, callback = s => s) => {
};

export const request = (configuration, method, path, options) => {
const { baseUrl, access_token } = configuration;
const { baseUrl, access_token } = configuration ?? {};

const { data, params = {} } = options;

Expand Down
48 changes: 5 additions & 43 deletions packages/mojatax/test/Adaptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,7 @@ before(() => {
.persist();
});

describe('execute', () => {
it('executes each operation in sequence', async () => {
const state = {
configuration,
};
const operations = [
state => {
return { counter: 1 };
},
state => {
return { counter: 2 };
},
state => {
return { counter: 3 };
},
];

const finalState = await execute(...operations)(state);

expect(finalState).to.eql({ counter: 3 });
});

it('assigns references and data to the initialState', async () => {
const state = {
configuration,
};

const finalState = await execute()(state);

expect(finalState).to.eql({
configuration: {
baseUrl: 'https://fake.mojatax.server.com',
clientId: 'someclientid',
password: 'somepassword',
access_token: 'fake-token',
},
references: [],
data: null,
});
});
});

describe('request', () => {
it('makes a post request to the right endpoint', async () => {
Expand Down Expand Up @@ -97,14 +57,16 @@ describe('request', () => {
});

describe('post', () => {
it('should create an invoice', async () => {
it('posts to /CreateInvoice', async () => {

testServer
.intercept({
path: `/api/v1/client/CreateInvoice`,
method: 'POST',
})

.reply(201, () => {
.reply(201, (req) => {
expect(JSON.parse(req.body)).to.haveOwnProperty('customerId');
return { id: 7,"customerId": "102", }
});

Expand All @@ -113,7 +75,7 @@ describe('post', () => {
};

const { data } = await execute(
post('CreateInvoice',{
post('CreateInvoice', {
"customerId": "102",
"invoice_id": "PID092",
} )
Expand Down

0 comments on commit 0769315

Please sign in to comment.