🧪 creted autmoated test suite

test user, harvest, and projects
This commit is contained in:
ysandler 2020-09-17 18:23:38 -05:00 committed by Joshua Shoemaker
parent d6987567c4
commit 51f7b89c94
18 changed files with 242 additions and 83 deletions

32
package-lock.json generated
View File

@ -285,11 +285,11 @@
"dev": true "dev": true
}, },
"axios": { "axios": {
"version": "0.20.0", "version": "0.19.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.20.0.tgz", "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
"integrity": "sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA==", "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
"requires": { "requires": {
"follow-redirects": "^1.10.0" "follow-redirects": "1.5.10"
} }
}, },
"balanced-match": { "balanced-match": {
@ -807,9 +807,27 @@
"dev": true "dev": true
}, },
"follow-redirects": { "follow-redirects": {
"version": "1.13.0", "version": "1.5.10",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
"integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==" "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
"requires": {
"debug": "=3.1.0"
},
"dependencies": {
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
}
}
}, },
"fs.realpath": { "fs.realpath": {
"version": "1.0.0", "version": "1.0.0",

View File

@ -27,7 +27,8 @@
"lint": "eslint src --ext ts", "lint": "eslint src --ext ts",
"watch": "tsc -watch -p ./", "watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint", "pretest": "npm run compile && npm run lint",
"test": "node ./out/test/runTest.js" "core-test": "ts-node ./src/test.suite/coreTests.ts",
"extention-test": "node ./out/test/runTest.js"
}, },
"devDependencies": { "devDependencies": {
"@types/axios": "^0.14.0", "@types/axios": "^0.14.0",
@ -44,6 +45,6 @@
"vscode-test": "^1.4.0" "vscode-test": "^1.4.0"
}, },
"dependencies": { "dependencies": {
"axios": "^0.20.0" "axios": "^0.19.0"
} }
} }

View File

@ -16,6 +16,10 @@ class Harvest {
return instance return instance
} }
destructor (): void {
instance = null
}
get headers () { get headers () {
return { return {
'Harvest-Account-ID': this.accountId, 'Harvest-Account-ID': this.accountId,

View File

@ -1,7 +1,7 @@
import TaskInterface from "./TaskInterface"; import TaskInterface from "./TaskInterface";
interface ProjectInterface { interface ProjectInterface {
id: string, id: number,
name: string, name: string,
tasks: TaskInterface[] tasks: TaskInterface[]
} }

View File

@ -1,5 +1,5 @@
interface TaskInterface { interface TaskInterface {
id: string, id: number,
name: string name: string
} }

View File

@ -2,7 +2,7 @@ import ProjectInterface from './Interfaces/ProjectInterface'
import TaskInterface from './Interfaces/TaskInterface' import TaskInterface from './Interfaces/TaskInterface'
class Project { class Project {
public id: string public id: number
public name: string public name: string
public tasks: TaskInterface[] public tasks: TaskInterface[]

View File

@ -10,6 +10,10 @@ class ProjectCollection {
return instance return instance
} }
destructor (): void {
instance = null
}
addOne = (project: Project): void => { addOne = (project: Project): void => {
this.elements.push(project) this.elements.push(project)
} }
@ -20,7 +24,7 @@ class ProjectCollection {
}) })
} }
findById = (id: string): Project | undefined => { findById = (id: number): Project | undefined => {
const project = this.elements.find((p: Project) => { const project = this.elements.find((p: Project) => {
return p.id === id return p.id === id
}) })

View File

@ -8,18 +8,22 @@ class User {
public readonly lastName: string public readonly lastName: string
public readonly email: string public readonly email: string
public readonly avatar: string public readonly avatar: string
constructor (props: UserInterface) { constructor (props?: UserInterface) {
if (!instance) instance = this if (!instance) instance = this
this.id = props.id this.id = props?.id || 0
this.firstName = props.firstName this.firstName = props?.firstName || ''
this.lastName = props.lastName this.lastName = props?.lastName || ''
this.email = props.email this.email = props?.email || ''
this.avatar = props.avatar this.avatar = props?.avatar || ''
return instance return instance
} }
destructor (): void {
instance = null
}
get props (): UserInterface { get props (): UserInterface {
return { return {
id: this.id, id: this.id,

View File

@ -2,10 +2,9 @@ import axios from 'axios'
import Harvest from '../Entities/Harvest' import Harvest from '../Entities/Harvest'
import ErrorMessage from '../Constants/ErrorMessageInterface' import ErrorMessage from '../Constants/ErrorMessageInterface'
import ErrorMessages from '../Constants/ErrorMessages' import ErrorMessages from '../Constants/ErrorMessages'
import Project from '../Entities/Project'
import ProjectInterface from '../Entities/Interfaces/ProjectInterface' import ProjectInterface from '../Entities/Interfaces/ProjectInterface'
const getProjects = async (): Promise<Project[] | ErrorMessage> => { const getProjects = async (): Promise<ProjectInterface[] | ErrorMessage> => {
const harvest = new Harvest() const harvest = new Harvest()
let projectsResponse: any let projectsResponse: any
try { try {
@ -18,7 +17,7 @@ const getProjects = async (): Promise<Project[] | ErrorMessage> => {
return ErrorMessages[1] return ErrorMessages[1]
} }
const projectData = projectsResponse.data.project_assignments.map((p: any) => { const projects = projectsResponse.data.project_assignments.map((p: any) => {
return { return {
id: p.project.id, id: p.project.id,
name: p.project.name, name: p.project.name,
@ -31,9 +30,6 @@ const getProjects = async (): Promise<Project[] | ErrorMessage> => {
} }
}) })
const projects = projectData.map((p: ProjectInterface) => {
return new Project(p)
})
return projects return projects
} }

View File

@ -2,9 +2,9 @@ import axios from 'axios'
import Harvest from '../Entities/Harvest' import Harvest from '../Entities/Harvest'
import ErrorMessage from '../Constants/ErrorMessageInterface' import ErrorMessage from '../Constants/ErrorMessageInterface'
import ErrorMessages from '../Constants/ErrorMessages' import ErrorMessages from '../Constants/ErrorMessages'
import User from "../Entities/User" import UserInterface from '../Entities/Interfaces/UserInterface'
const getUser = async (): Promise<User | ErrorMessage> => { const getUser = async (): Promise<UserInterface | ErrorMessage> => {
const harvest = new Harvest() const harvest = new Harvest()
let userResponse: any let userResponse: any
try { try {
@ -17,7 +17,7 @@ const getUser = async (): Promise<User | ErrorMessage> => {
return ErrorMessages[0] return ErrorMessages[0]
} }
const userData = { const user = {
id: userResponse.data.id || '', id: userResponse.data.id || '',
firstName: userResponse.data.first_name || '', firstName: userResponse.data.first_name || '',
lastName: userResponse.data.last_name || '', lastName: userResponse.data.last_name || '',
@ -25,7 +25,6 @@ const getUser = async (): Promise<User | ErrorMessage> => {
avatar: userResponse.data.avatar_url || '', avatar: userResponse.data.avatar_url || '',
} }
const user = new User(userData)
return user return user
} }

View File

@ -0,0 +1,6 @@
interface UnitTest {
name: string,
test: Function
}
export default UnitTest

View File

@ -0,0 +1,27 @@
import Harvest from '../../Entities/Harvest'
import UnitTest from '../UnitTestInterface'
const harvestCreateInstance = (): boolean => {
const input = {
accountId: '123',
accessToken: 'XYZ',
organization: 'ABC'
}
const expectedOutput = {
accountId: '123',
accessToken: 'XYZ',
organization: 'ABC'
}
const harvest = new Harvest(input)
if (JSON.stringify(harvest.props) === JSON.stringify(expectedOutput)) return true
else return false
}
const unitTests: UnitTest[] = [
{ name: 'Entity | Harvest Create Instance', test: harvestCreateInstance }
]
export default unitTests

View File

@ -0,0 +1,61 @@
import ErrorMessage from "../../Constants/ErrorMessageInterface"
import Harvest from "../../Entities/Harvest"
import ProjectInterface from "../../Entities/Interfaces/ProjectInterface"
import Project from "../../Entities/Project"
import getProjects from '../../UseCases/getProjects'
import env from '../env'
import UnitTest from "../UnitTestInterface"
const projectCreateInstance = async (): Promise<boolean> => {
const input: ProjectInterface = {
id: 10203,
name: 'Test Project',
tasks: [{
id: 123,
name: 'Test Task'
}]
}
const expectedOutput: ProjectInterface = {
id: 10203,
name: 'Test Project',
tasks: [{
id: 123,
name: 'Test Task'
}]
}
const project = new Project(input)
if (JSON.stringify(project.props) === JSON.stringify(expectedOutput)) return true
else return false
}
const getProjectsFromApi = async (): Promise<boolean> => {
new Harvest().destructor()
new Harvest({
accountId: env.accountId,
accessToken: env.accessToken,
organization: 'ABC'
})
const expectedOutput: ProjectInterface = env.firstProjectFromApi
const projects: ProjectInterface[] | ErrorMessage = await getProjects()
if (!Array.isArray(projects)) return false
const project: ProjectInterface = projects[0]
// based off my own user data
if (JSON.stringify(project) === JSON.stringify(expectedOutput)) return true
else return false
}
const unitTests: UnitTest[] = [
{ name: 'Entity | Project Create Instance', test: projectCreateInstance },
{ name: 'Use Case | Get Projects From Api', test: getProjectsFromApi },
]
export default unitTests

View File

@ -0,0 +1,57 @@
import Harvest from "../../Entities/Harvest"
import UserInterface from "../../Entities/Interfaces/UserInterface"
import User from "../../Entities/User"
import getUser from '../../UseCases/getUser'
import env from '../env'
import UnitTest from "../UnitTestInterface"
const userCreateInstance = (): boolean => {
new User().destructor()
const input: UserInterface = {
id: 10203,
firstName: 'Joshua',
lastName: 'Sandler',
email: 'joshua@me.com',
avatar: 'link'
}
const expectedOutput: UserInterface = {
id: 10203,
firstName: 'Joshua',
lastName: 'Sandler',
email: 'joshua@me.com',
avatar: 'link'
}
const user = new User(input)
if(JSON.stringify(user.props) === JSON.stringify(expectedOutput)) return true
else return false
}
const getUserFromApi = async (): Promise<boolean> => {
new Harvest().destructor()
new User().destructor()
new Harvest({
accountId: env.accountId,
accessToken: env.accessToken,
organization: 'ABC'
})
// this data is based off my own user
const expectedOutput: UserInterface = env.userFromApi
const userData = await getUser()
if (JSON.stringify(userData) === JSON.stringify(expectedOutput)) return true
else return false
}
const unitTests: UnitTest[] = [
{ name: 'Entity | User Create Instance', test: userCreateInstance },
{ name: 'Use Case | Get User from Api', test: getUserFromApi }
]
export default unitTests

35
src/test/coreTests.ts Normal file
View File

@ -0,0 +1,35 @@
import HarvestTests from './core/HarvestTests'
import UserTests from './core/UserTests'
import ProjectsTests from './core/ProjectsTests'
import UnitTest from './UnitTestInterface'
async function runTestsAndReturnFailures(tests: UnitTest[]): Promise<string[]> {
const testTotalCount = tests.length
const testsFailed: string[] = []
for (let i = 0; i < testTotalCount; i++) {
const didPassTest = await tests[i].test()
if (!didPassTest) testsFailed.push(tests[i].name)
}
return testsFailed
}
async function init(tests: UnitTest[]) {
const failedTestsResults = await runTestsAndReturnFailures(tests)
if (failedTestsResults.length === 0) {
console.log('\x1b[32m%s\x1b[0m', 'All Tests Passed!!')
} else {
console.log(`\x1b[31mFailed ${failedTestsResults.length} tests.\x1b[0m`)
failedTestsResults.forEach((test) => {
console.log(`\x1b[33m${test}\x1b[0m`)
})
}
}
const testsArray: any = [
HarvestTests,
UserTests,
ProjectsTests
]
init(testsArray.flat())

View File

@ -1,14 +0,0 @@
import Harvest from '../../Entities/Harvest'
const harvest = new Harvest({
accountId: '123',
accessToken: 'XYZ'
})
console.log(harvest.props)
const harvest2 = new Harvest()
harvest2.organization = 'ABC'
console.log(harvest2.props)

View File

@ -1,23 +0,0 @@
import ErrorMessage from "../../Constants/ErrorMessageInterface"
import Harvest from "../../Entities/Harvest"
import Project from "../../Entities/Project"
import getProjects from '../../UseCases/getProjects'
import env from '../env'
async function test () {
new Harvest({
accountId: env.accountId,
accessToken: env.accessToken,
organization: 'ABC'
})
const projects: Project[] | ErrorMessage = await getProjects()
if (Array.isArray(projects)) {
console.log(projects[0])
console.log(`${projects.length} Prpjects found`)
}
else console.log(projects)
}
test()

View File

@ -1,16 +0,0 @@
import Harvest from "../../Entities/Harvest"
import getUser from '../../UseCases/getUser'
import env from '../env'
async function test () {
new Harvest({
accountId: env.accountId,
accessToken: env.accessToken,
organization: 'ABC'
})
const user = await getUser()
console.log(user)
}
test()