import { TestBed } from '@angular/core/testing';
import { HttpClientModule, HttpRequest } from '@angular/common/http';
import { HttpTestingController, HttpClientTestingModule } from '@angular/common/http/testing';
import { ISoapMethodResponse } from 'ngx-soap/ngx-soap';
import { NgxSoapService } from './ngx-soap.service';
import { NgxSoapModule } from './ngx-soap.module';
const PROXIED_CALCULATOR_WSDL = `
Adds two integers. This is a test WebService. ©DNE Online
`;
let service: NgxSoapService;
let httpMock: HttpTestingController;
describe('NgxSoapService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, HttpClientTestingModule, NgxSoapModule],
providers: [NgxSoapService]
});
service = TestBed.inject(NgxSoapService);
httpMock = TestBed.inject(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should create client from calculator.wsdl', () => {
service.createClient('/calculator.wsdl', { disableCache: true })
.then(client => {
expect(client).toBeTruthy();
client.call('Add', { intA: 3, intB: 2 }, { exchangeId: '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000' })
})
.catch(err => console.log('Error', err));
const req = httpMock.expectOne('/calculator.wsdl');
expect(req.request.method).toBe("GET");
req.flush(PROXIED_CALCULATOR_WSDL);
});
it('should create client from calculator.wsdl', () => {
service.createClient('/calculator.wsdl', { disableCache: true })
.then(client => expect(client).toBeTruthy())
.catch(err => console.log('Error', err));
let req = httpMock.expectOne('/calculator.wsdl');
req.flush(PROXIED_CALCULATOR_WSDL);
httpMock.verify();
});
it('3 + 2 should be equal to 5', () => {
service.createClient('/calculator.wsdl', { disableCache: true })
.then(client => {
client.call('Add', {
intA: 3,
intB: 2
}, { exchangeId: '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000' }).subscribe((soapResponse: ISoapMethodResponse) => {
expect(soapResponse.result.AddResult).toBe(5);
}, err => console.error(err));
let req = httpMock.expectOne('/calculator/calculator.asmx');
let res = {
headers: {},
body: ``
};
req.flush('5');
httpMock.verify();
})
.catch(err => console.log('Error', err));
let req = httpMock.expectOne('/calculator.wsdl');
expect(req.request.method).toBe("GET");
req.flush(PROXIED_CALCULATOR_WSDL);
});
it('should raise an error when calling a missing operation', () => {
service
.createClient('/calculator.wsdl', { disableCache: true })
.then(client => {
const method = 'NonExistingMethod';
client
.call(method, {}, { exchangeId: '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000' })
.subscribe(() => { }, err => expect(err).toBe(`Method ${method} not found`));
});
const req = httpMock.expectOne('/calculator.wsdl');
expect(req.request.method).toBe("GET");
req.flush(PROXIED_CALCULATOR_WSDL);
});
});