Skip to content

Commit

Permalink
feat: improve testing (#30)
Browse files Browse the repository at this point in the history
* feat(repo): adding npm skel files

* feat(class): core function

* feat(deps): update packages

* Update README.md

* feat(core): finished initial commit

* Update tslint.json

* Create npm-publish.yml

* Update npm-publish.yml

* Update package.json

* Update package.json

* Update package-lock.json

* Update npm-publish.yml

* Update README.md

* chore: remove test package

* Update package.json

* Update package.json

* feat(test): added jest test and test ci

* chore(deps): update package

* chore(core): refactored

* chore(test): fix error

* Update tsconfig.json

* Update tsconfig.json

* Update package.json

* Update tsconfig.json

* Update jest-test.yml

* Update jest-test.yml

* Update index.test.ts

* Update jest-test.yml

* Update jest-test.yml

* Update jest-test.yml

* Update jest-test.yml

* Update jest-test.yml

* Update jest-test.yml

* chore(workflows): updated ci

* Update jest-test.yml

* Update jest-test.yml

* chore(workflows): add coverage

* Update jest-test.yml

* Update jest-test.yml

* Update jest-test.yml

* Update jest-test.yml

* feat(workflow): separate coverage ci

* Update index.test.ts

* Create CODE_OF_CONDUCT.md

* Update package.json

* Update README.md

* Update index.ts

* Update npm-publish.yml

- fix typo

* Update package.json

- bump version

* fix: exclude dist file on test

* bump: v0.0.8

* bump: version 0.1.0

- rename to adasms-sdk

* fix: package.json

- update main and types

* fix: exports

- bump v0.1.2

* chore: update lock

* fix: tests

* chore: redefine exports

* bump: v0.1.3

* Update tsconfig.json

* bump: v0.1.4

* bump: v0.1.4

* fix: fix import issue

- remove type
- bump version

* chore: linting

- update tsconfig
- bump v0.1.6
- update format script

* fix: import issue

* v0.1.9

- update test
- bump v0.1.9
- update readme
- update token handling
- cleanup jest config

* feat: coverage report

- bump v0.1.10

* feat: added test for error branch

- bump v0.1.11
- update readme
- refact main.ts
- refact types.ts

* chore: remove secrets from test

* chore: update tests

* feat: update test randomness

* fix: test failing

* fix: fail test

* fix: fail test

* fix: temp comment failing test

* Update index.test.ts

* chore: readme coverage
  • Loading branch information
neko1101 authored Nov 12, 2022
1 parent 7d28b52 commit 68b30d6
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/jest-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run test -- --verbose --coverage
- run: npm run test -- --verbose --maxWorkers=1
env:
TEST_PHONE: ${{secrets.TEST_PHONE}}
ADASMS_APPLICATION_SECRET: ${{secrets.ADASMS_APPLICATION_SECRET}}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This is Non-Official a [AdaSMS](https://adasms.com/register) SDK.

| Statements | Branches | Functions | Lines |
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
| ![Statements](https://img.shields.io/badge/statements-76.47%25-red.svg?style=flat&logo=jest) | ![Branches](https://img.shields.io/badge/branches-54.83%25-red.svg?style=flat&logo=jest) | ![Functions](https://img.shields.io/badge/functions-100%25-brightgreen.svg?style=flat&logo=jest) | ![Lines](https://img.shields.io/badge/lines-76.47%25-red.svg?style=flat&logo=jest) |
| ![Statements](https://img.shields.io/badge/statements-71.96%25-red.svg?style=flat&logo=jest) | ![Branches](https://img.shields.io/badge/branches-54.54%25-red.svg?style=flat&logo=jest) | ![Functions](https://img.shields.io/badge/functions-90.9%25-brightgreen.svg?style=flat&logo=jest) | ![Lines](https://img.shields.io/badge/lines-71.96%25-red.svg?style=flat&logo=jest) |


## AdaSMS
Expand Down
182 changes: 161 additions & 21 deletions __tests__/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Client, * as type from "../../src/index"
import {expectTypeOf} from 'expect-type'
import moment from 'moment'
import { v4 as uuidv4 } from 'uuid'

let phone = process.env.TEST_PHONE
let token = process.env.ADASMS_APPLICATION_SECRET
Expand Down Expand Up @@ -39,6 +40,19 @@ it('should send sms with preview mode', async () => {
expect(response.message!.preview).toBe(true)
});

it('should return send sms with error', async () => {
const params: type.SendSMSObject = {
message: "Hello from jest test",
previewMode: true,
lead_id: 1
}
const response = await client.sendSMS(params)
expectTypeOf(response).toMatchTypeOf<type.SendSMSResponse>()
expect(response.success).toBeFalsy()
expect(response.error).toBeDefined()
expect(response.explain).toBeDefined()
});

it('should send sms wit callback', async () => {
const params: type.SendSMSObject = {
phone: phone,
Expand All @@ -51,17 +65,29 @@ it('should send sms wit callback', async () => {
expect(response.success).toBe(true)
});

// it('should send sms with lead_id', async () => {
// const params: type.SendSMSObject = {
// message: "Hello from jest test",
// previewMode: true,
// lead_id: 1
// }
// const response = await client.sendSMS(params)
// expectTypeOf(response).toMatchTypeOf<type.SendSMSResponse>()
// expect(response.message!.lead_id).toBe("1")
// console.log(response)
// });
it('should send sms with lead_id', async () => {
const leadParams: type.CreateLeadOption = {
name: "test lead"
}
const lead = await client.createLead(leadParams)

const createContactParam: type.CreateContactOption = {
phone: "60199131212",
lead_id: lead.id!
}
await client.createContact(createContactParam)

const params: type.SendSMSObject = {
message: "Hello from jest test",
previewMode: true,
lead_id: lead.id!
}
const response = await client.sendSMS(params)
expectTypeOf(response).toMatchTypeOf<type.SendSMSResponse>()
expect(response.message!.lead_id).toBe(lead.id!.toString())

await cleanupLead({ lead_id: lead.id!.toString()})
});

it('should send sms with send_at', async () => {
const params: type.SendSMSObject = {
Expand All @@ -75,12 +101,38 @@ it('should send sms with send_at', async () => {
expect(response.message?.send_at).toBe(params.send_at + ":00")
});

it('should return error when send sms with invalid send_at format', async () => {
const params: type.SendSMSObject = {
phone: phone,
message: "Hello from jest test",
previewMode: true,
send_at: moment().add(1, 'day').format("YYYY:MM:DD") // Y-m-d H:i
}
const response = await client.sendSMS(params)
expectTypeOf(response).toMatchTypeOf<type.SendSMSResponse>()
expect(response.error).toBeDefined()
expect(response.success).toBeFalsy()
expect(response.explain).toBeDefined()
});

it('should check for account balance', async () => {
const response = await client.getCreditBalance()
expectTypeOf(response).toMatchTypeOf<type.BalanceResponse>()
expect(response.balance).toBeDefined()
});

// #failing on ci
// it('should return error when account balance', async () => {
// const option: type.ClientOption = {
// token: randomToken()
// }
// const client2 = new Client(option)
// const response = await client2.getCreditBalance()
// expectTypeOf(response).toMatchTypeOf<type.BalanceResponse>()
// expect(response.error).toBeDefined()
// expect(response.explain).toBeDefined()
// });

it('should create lead', async () => {
const params: type.CreateLeadOption = {
name: "test lead"
Expand All @@ -102,37 +154,79 @@ it('should delete lead_id', async () => {
lead_id: lead.id!
}
const response = await client.deleteLead(params)
expectTypeOf(response).toMatchTypeOf<type.DeleteContactResponse>()
expectTypeOf(response).toMatchTypeOf<type.DeleteLeadResponse>()
expect(response.success).toBeTruthy()

await cleanupLead({ lead_id: lead.id!.toString()})
});

it('should list leads', async () => {
it('should return error when delete lead_id', async () => {
const params: type.DeleteLeadOption = {
lead_id: 9999
}
const response = await client.deleteLead(params)
expectTypeOf(response).toMatchTypeOf<type.DeleteLeadResponse>()
expect(response.success).toBeFalsy()
expect(response.error).toBeDefined()
});

// #failing on ci
// it('should list leads', async () => {
// const leadParams: type.CreateLeadOption = {
// name: "test lead"
// }
// const lead = await client.createLead(leadParams)

// const response = await client.getLeads()
// expectTypeOf(response).toMatchTypeOf<type.GetLeadResponse[] | type.ErrorResponse>()
// // @ts-ignore
// expect(response.length).toBeGreaterThan(0)

// await cleanupLead({lead_id: lead.id!.toString()})
// });

// #failing on ci
// it('should return error when list leads', async () => {
// const option: type.ClientOption = {
// token: randomToken()
// }
// const client3 = new Client(option)
// const response = await client3.getLeads()
// expectTypeOf(response).toMatchTypeOf<type.GetLeadResponse>()
// expect(response.error).toBeDefined()
// expect(response.explain).toBeDefined()
// });

it('should add contact in lead', async () => {
const leadParams: type.CreateLeadOption = {
name: "test lead"
}
const lead = await client.createLead(leadParams)

const response = await client.getLeads()
expectTypeOf(response).toMatchTypeOf<type.GetLeadResponse[] | type.ErrorResponse>()
// @ts-ignore
expect(response.length).toBeGreaterThan(0)
const params: type.CreateContactOption = {
phone: "60199131212",
lead_id: lead.id!
}
const response = await client.createContact(params)
expectTypeOf(response).toMatchTypeOf<type.CreateContactResponse>()
expect(response.contact_id).toBeDefined()

await cleanupLead({lead_id: lead.id!.toString()})
});

it('should add contact in lead', async () => {
it('should return error when add contact in lead', async () => {
const leadParams: type.CreateLeadOption = {
name: "test lead"
}
const lead = await client.createLead(leadParams)

const params: type.CreateContactOption = {
phone: "60199131212",
phone: "123",
lead_id: lead.id!
}
const response = await client.createContact(params)
expectTypeOf(response).toMatchTypeOf<type.CreateContactResponse>()
expect(response.contact_id).toBeDefined()
expect(response.error).toBeDefined()

await cleanupLead({lead_id: lead.id!.toString()})
});
Expand Down Expand Up @@ -160,6 +254,15 @@ it('should get contacts for lead_id', async () => {
await cleanupLead({lead_id: lead.id!.toString()})
});

it('should return error when get contacts for lead_id', async () => {
const params: type.GetContactListOption = {
lead_id: 123
}
const response = await client.getContactList(params)
expectTypeOf(response).toMatchTypeOf<type.GetContactListResponse>()
expect(response.error).toBeDefined()
});

it('should delete contact in lead_id', async () => {
const leadParams: type.CreateLeadOption = {
name: "test lead"
Expand All @@ -183,6 +286,16 @@ it('should delete contact in lead_id', async () => {
await cleanupLead({lead_id: lead.id!.toString()})
});

it('should return error when delete contact in lead_id', async () => {
const params: type.DeleteContactOption = {
lead_id: 100,
contact_id: 123
}
const response = await client.deleteContact(params)
expectTypeOf(response).toMatchTypeOf<type.DeleteContactResponse>()
expect(response.error).toBeDefined()
});

it('should able to delete scheduled message', async () => {
const sendSMSParams: type.SendSMSObject = {
phone: phone,
Expand All @@ -200,6 +313,15 @@ it('should able to delete scheduled message', async () => {
expect(response.success).toBeTruthy()
});

it('should return error when delete scheduled message', async () => {
const params: type.DeleteMessageObject = {
message_id: 123
}
const response = await client.deleteScheduledMessage(params)
expectTypeOf(response).toMatchTypeOf<type.DeleteScheduledMessageResponse>()
expect(response.error).toBeDefined()
});

it('should list scheduled messages', async () => {
const sendSMSParams: type.SendSMSObject = {
phone: phone,
Expand All @@ -214,6 +336,19 @@ it('should list scheduled messages', async () => {
await cleanupScheduledSMS({message_id: sms.message!.scheduled_message_id})
});

// #failing on ci
// it('should return error when list scheduled messages', async () => {
// const option: type.ClientOption = {
// token: randomToken()
// }
// const client1 = new Client(option)

// const response = await client1.listScheduledMessage()
// expectTypeOf(response).toMatchTypeOf<type.ListScheduledMessageResponse>()
// expect(response.error).toBeDefined()
// expect(response.error).toBeDefined()
// });

const cleanupScheduledSMS = async (params: { message_id: number}) => {
const deleteParams: type.DeleteMessageObject = {
message_id: params.message_id
Expand All @@ -233,5 +368,10 @@ const cleanupLead = async (params: { lead_id:string }) => {
token: token!
}
const deleteLeadClient = new Client(option)
await deleteLeadClient.deleteLead(deleteParams)
const response = await deleteLeadClient.deleteLead(deleteParams)
}

const randomToken = () => {
const token = uuidv4().replace("-","")
return token.toString()
}
35 changes: 32 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 68b30d6

Please sign in to comment.