Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
implementing conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
aguacongas committed Aug 28, 2016
1 parent 8525c29 commit 7e10aac
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
30 changes: 26 additions & 4 deletions src/chatle/app/shared/chat.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
import { ChatService } from "./chat.service";
import { Injector } from '@angular/core';
import { TestBed, inject } from '@angular/core/testing';
import { HttpModule, Http, XHRBackend } from '@angular/http';
import { MockBackend } from '@angular/http/testing';

import { ChatService, ConnectionState } from "./chat.service";

import { Settings } from './settings';
import { User } from './user';
import { Message } from './message';
import { Conversation } from './conversation';

describe('ChatService', () => {
it("start should return connectionState observable", () => {
let settings = new Settings();


beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
{ provide: XHRBackend, useClass: MockBackend }
]
});
});

it("start should return connectionState observable", inject([Settings, Http], (settings: Settings, http: Http) => {
let service = new ChatService(settings, http);
let connectionState: ConnectionState;

service.start(true)
.subscribe((response: ConnectionState) => {
connectionState = response;
});

expect(connectionState).toBe(ConnectionState.Connected);
}));
});
39 changes: 38 additions & 1 deletion src/chatle/app/shared/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,47 @@ export class ChatService {
// start the connection
$.connection.hub.start()
.done(response => this.connectionStateSubject.next(ConnectionState.Connected))
.fail(response => this.connectionStateSubject.error(ConnectionState.Error));
.fail(error => this.connectionStateSubject.error(error));

return this.connectionState;
};

createConversation(to: User, message: Message): Observable<Conversation> {
let conversationSubjet = new Subject<Conversation>();

this.http.post(this.settings.convAPI, {
to: to.id,
message: message
}).toPromise()
.then(response => {
let conversation = new Conversation();
let data = response.json();
conversation.id = data.id;
conversation.messages.unshift(message);
conversation.attendees.unshift(to);
conversationSubjet.next(conversation);
})
.catch(error => {
conversationSubjet.error(error);
});

return conversationSubjet.asObservable();
}

sendMessage(message: Message): Observable<Message> {
let messageSubject = new Subject<Message>();

this.http.post(this.settings.chatAPI, message)
.toPromise()
.then(response => {
messageSubject.next(message);
})
.catch(error => {
messageSubject.error(error);
});

return messageSubject.asObservable();
}

private onReconnected() {
this.connectionStateSubject.next(ConnectionState.Connected);
Expand Down
2 changes: 1 addition & 1 deletion src/chatle/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ paths.concatCssDest = paths.webroot + "css/site.min.css";

paths.lib = paths.webroot + "lib/";

paths.angular = paths.node_modules + "@angular/**/bundles/*.js"
paths.angular = paths.node_modules + "@angular/**/*.js"
paths.angularWebApi = paths.node_modules + "angular2-in-memory-web-api/*.js"
paths.corejs = paths.node_modules + "core-js/client/shim*.js";
paths.zonejs = paths.node_modules + "zone.js/dist/zone*.js";
Expand Down
6 changes: 4 additions & 2 deletions src/chatle/wwwroot/unit-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
<script src="/lib/jasmine-core/boot.js"></script>
<script src="/lib/zone.js/dist/zone.js"></script>
<script src="/lib/reflect-metadata/Reflect.js"></script>

</head>
<body>
<script src="/lib/systemjs/dist/system.src.js"></script>
<script>System.packageWithIndex = true;</script>
<script src="/js/systemjs.config.js"></script>

<script>
System.import('app/app.component.spec')
<script>
System.import('app/shared/chat.service.spec')
.then(window.onload)
.catch(console.error.bind(console));
</script>
Expand Down

0 comments on commit 7e10aac

Please sign in to comment.