-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get contracts local server and service (#42)
- Loading branch information
1 parent
785b313
commit 3488b67
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { | ||
Request, | ||
Response | ||
} from 'express' | ||
import { | ||
CONTRACTS | ||
} from './contracts' | ||
|
||
export const contractRoutes = { | ||
getContracts: function (req: Request, res: Response): void { | ||
res.status(200).json(CONTRACTS) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const CONTRACTS = [ | ||
{ | ||
id: 'contract_1', | ||
number: 'APX100', | ||
conditions: 'one year labour contract' | ||
}, | ||
{ | ||
id: 'contract_2', | ||
number: 'APX200', | ||
conditions: 'two year labour contract' | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Contract { | ||
id: string | ||
number: string | ||
conditions: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
TestBed | ||
} from '@angular/core/testing' | ||
|
||
import { | ||
ContractService | ||
} from './contract.service' | ||
import { | ||
HttpClientTestingModule, | ||
HttpTestingController | ||
} from '@angular/common/http/testing' | ||
import { | ||
Contract | ||
} from '../../interfaces/contract' | ||
|
||
describe('ContractService', () => { | ||
let service: ContractService | ||
let httpTestingController: HttpTestingController | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule] | ||
}) | ||
service = TestBed.inject(ContractService) | ||
httpTestingController = TestBed.inject(HttpTestingController) | ||
}) | ||
|
||
afterEach(() => { | ||
httpTestingController.verify() | ||
}) | ||
|
||
it('getContracts dispatches request properly and returns expected data', () => { | ||
const expectedContracts: Contract[] = [ | ||
{ | ||
id: 'test_a', | ||
number: 'testA', | ||
conditions: 'test a' | ||
}, | ||
{ | ||
id: 'test_b', | ||
number: 'testB', | ||
conditions: 'test b' | ||
} | ||
] | ||
let actualContracts: Contract[] = [] | ||
|
||
service.getContracts().subscribe((contracts) => { | ||
actualContracts = contracts | ||
}) | ||
const testRequest = httpTestingController.expectOne('/api/contracts') | ||
expect(testRequest.request.method).toBe('GET') | ||
|
||
testRequest.flush(expectedContracts) | ||
expect(actualContracts.length).toBe(expectedContracts.length) | ||
expectedContracts.forEach((expectedContract) => { | ||
expect(actualContracts).toContain(jasmine.objectContaining(expectedContract)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { | ||
HttpClient | ||
} from '@angular/common/http' | ||
import { | ||
Injectable | ||
} from '@angular/core' | ||
import { | ||
Observable | ||
} from 'rxjs' | ||
import { | ||
Contract | ||
} from '../../interfaces/contract' | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ContractService { | ||
public constructor(private http: HttpClient) { } | ||
|
||
public getContracts(): Observable<Contract[]> { | ||
return this.http.get<Contract[]>('/api/contracts') | ||
} | ||
} |