Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Show notifications to host when sites join or leave
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Scandurra committed Sep 27, 2017
1 parent 43babf6 commit 69dea87
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
10 changes: 8 additions & 2 deletions lib/host-portal-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ class HostPortalBinding {
this.portal.dispose()
}

siteDidJoin () {}
siteDidJoin (siteId) {
const {username} = this.portal.getSiteIdentity(siteId)
this.notificationManager.addInfo(`@${username} has joined your portal`)
}

siteDidLeave () {}
siteDidLeave (siteId) {
const {username} = this.portal.getSiteIdentity(siteId)
this.notificationManager.addInfo(`@${username} has left your portal`)
}

async didChangeActiveTextEditor (editor) {
if (editor == null) {
Expand Down
13 changes: 13 additions & 0 deletions test/helpers/atom-environments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const environments = []

exports.buildAtomEnvironment = function buildAtomEnvironment () {
const env = global.buildAtomEnvironment()
environments.push(env)
return env
}

exports.destroyAtomEnvironments = function destroyAtomEnvironments () {
const destroyPromises = environments.map((e) => e.destroy())
environments.length = 0
return Promise.all(destroyPromises)
}
61 changes: 50 additions & 11 deletions test/host-portal-binding.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,67 @@
const assert = require('assert')
const {buildAtomEnvironment, destroyAtomEnvironments} = require('./helpers/atom-environments')
const {RealTimeClient} = require('@atom/real-time-client')
const HostPortalBinding = require('../lib/host-portal-binding')
const FakeClipboard = require('./helpers/fake-clipboard')

suite('HostPortalBinding', () => {
teardown(async () => {
await destroyAtomEnvironments()
})

test('handling an unexpected error when joining a portal', async () => {
const client = new RealTimeClient({})
client.createPortal = function () {
throw new Error('It broke!')
}
const notificationManager = {
errors: [],
addError (...args) { this.errors.push(args) }
}
const portalBinding = new HostPortalBinding({
client,
notificationManager
})
const atomEnv = buildAtomEnvironment()
const portalBinding = buildHostPortalBinding(client, atomEnv)

const result = await portalBinding.initialize()
assert.equal(result, false)

assert.equal(notificationManager.errors.length, 1)
const [[message, params]] = notificationManager.errors
assert.equal(atomEnv.notifications.getNotifications().length, 1)
const {message, options} = atomEnv.notifications.getNotifications()[0]
assert.equal(message, 'Failed to share portal')
assert(params.description.includes('It broke!'))
assert(options.description.includes('It broke!'))
})

test('showing notifications when sites join or leave', async () => {
const client = new RealTimeClient({})
const portal = {
setDelegate (delegate) {
this.delegate = delegate
},
getSiteIdentity (siteId) {
return {username: 'site-' + siteId}
},
setActiveEditorProxy () {}
}
client.createPortal = function () {
return portal
}
const atomEnv = buildAtomEnvironment()
const portalBinding = buildHostPortalBinding(client, atomEnv)
await portalBinding.initialize()

atomEnv.notifications.clear()
portal.delegate.siteDidJoin(2)
assert.equal(atomEnv.notifications.getNotifications().length, 1)
assert(atomEnv.notifications.getNotifications()[0].message.includes('@site-2'))

atomEnv.notifications.clear()
portal.delegate.siteDidLeave(3)
assert.equal(atomEnv.notifications.getNotifications().length, 1)
assert(atomEnv.notifications.getNotifications()[0].message.includes('@site-3'))
})

function buildHostPortalBinding (client, atomEnv) {
return new HostPortalBinding({
client,
notificationManager: atomEnv.notifications,
workspace: atomEnv.workspace,
clipboard: new FakeClipboard(),
addStatusBarIndicatorForPortal: () => {}
})
}
})
12 changes: 2 additions & 10 deletions test/real-time-package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const RealTimePackage = require('../lib/real-time-package')
const {Errors} = require('@atom/real-time-client')
const {TextBuffer, TextEditor} = require('atom')

const {buildAtomEnvironment, destroyAtomEnvironments} = require('./helpers/atom-environments')
const assert = require('assert')
const condition = require('./helpers/condition')
const deepEqual = require('deep-equal')
Expand Down Expand Up @@ -35,7 +36,6 @@ suite('RealTimePackage', function () {
})

setup(() => {
environments = []
packages = []
containerElement = document.createElement('div')
document.body.appendChild(containerElement)
Expand All @@ -49,9 +49,7 @@ suite('RealTimePackage', function () {
for (const pack of packages) {
await pack.dispose()
}
for (const env of environments) {
await env.destroy()
}
await destroyAtomEnvironments()
})

test('sharing and joining a portal', async function () {
Expand Down Expand Up @@ -762,12 +760,6 @@ suite('RealTimePackage', function () {
assert(description.includes('connection-error'))
})

function buildAtomEnvironment () {
const env = global.buildAtomEnvironment()
environments.push(env)
return env
}

function buildPackage (env) {
const pack = new RealTimePackage({
baseURL: testServer.address,
Expand Down

1 comment on commit 69dea87

@mciek
Copy link

@mciek mciek commented on 69dea87 Sep 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.