From 024230c7f4792d9f6fb9b899b1c9f8738dbacce2 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 15 Mar 2017 16:22:36 +0100 Subject: Basic word position and completion service. --- .../specs/services/completion/completion.spec.js | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 test/unit/specs/services/completion/completion.spec.js (limited to 'test') diff --git a/test/unit/specs/services/completion/completion.spec.js b/test/unit/specs/services/completion/completion.spec.js new file mode 100644 index 00000000..8a41c653 --- /dev/null +++ b/test/unit/specs/services/completion/completion.spec.js @@ -0,0 +1,70 @@ +import { replaceWord, addPositionToWords, wordAtPosition, splitIntoWords } from '../../../../../src/services/completion/completion.js' + +describe('addPositiontoWords', () => { + it('adds the position to a word list', () => { + const words = ['hey', 'this', 'is', 'fun'] + + const expected = [ + { + word: 'hey', + start: 0, + end: 3 + }, + { + word: 'this', + start: 3, + end: 7 + }, + { + word: 'is', + start: 7, + end: 9 + }, + { + word: 'fun', + start: 9, + end: 12 + } + ] + + const res = addPositionToWords(words) + + expect(res).to.eql(expected) + }) +}) + +describe('splitIntoWords', () => { + it('splits at whitespace boundaries', () => { + const str = 'This is a #nice @test for you, @idiot.' + const expected = ['This', ' ', 'is', ' ', 'a', ' ', '#nice', ' ', '@test', ' ', 'for', ' ', 'you', ', ', '@idiot', '.'] + const res = splitIntoWords(str) + + expect(res).to.eql(expected) + }) +}) + +describe('wordAtPosition', () => { + it('returns the word for a given string and postion, plus the start and end position of that word', () => { + const str = 'Hey this is fun' + + const { word, start, end } = wordAtPosition(str, 4) + + expect(word).to.eql('this') + expect(start).to.eql(4) + expect(end).to.eql(8) + }) +}) + +describe('replaceWord', () => { + it('replaces a word (with start and end) with another word in a given string', () => { + const str = 'hey @take, how are you' + const wordsWithPosition = addPositionToWords(splitIntoWords(str)) + const toReplace = wordsWithPosition[2] + + expect(toReplace.word).to.eql('@take') + + const expected = 'hey @takeshitakenji, how are you' + const res = replaceWord(str, toReplace, '@takeshitakenji') + expect(res).to.eql(expected) + }) +}) -- cgit v1.2.3-70-g09d2 From 72de959221b44eb3d3533fd5624957cb1d29f058 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 6 Jun 2017 15:54:08 +0200 Subject: Remove notifications for deleted messages. --- src/modules/statuses.js | 4 ++++ test/unit/specs/modules/statuses.spec.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'test') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 051ec71b..98a8d16d 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -242,6 +242,10 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us const uri = deletion.uri updateMaxId(deletion) + // Remove possible notification + const status = find(allStatuses, {uri}) + remove(state.notifications, ({action: {id}}) => status.id) + remove(allStatuses, { uri }) if (timeline) { remove(timelineObject.statuses, { uri }) diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index 891423ca..66d6cfd7 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -319,6 +319,34 @@ describe('The Statuses module', () => { expect(state.notifications[0].type).to.eql('mention') }) + it('removes a notification when the notice gets removed', () => { + const user = { id: 1 } + const state = cloneDeep(defaultState) + const status = makeMockStatus({id: 1}) + const mentionedStatus = makeMockStatus({id: 2}) + mentionedStatus.attentions = [user] + mentionedStatus.uri = 'xxx' + + const deletion = makeMockStatus({id: 3, is_post_verb: false}) + deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.' + deletion.uri = 'xxx' + + mutations.addNewStatuses(state, { statuses: [status], user }) + + expect(state.notifications.length).to.eql(0) + + mutations.addNewStatuses(state, { statuses: [mentionedStatus], user }) + expect(state.allStatuses.length).to.eql(2) + expect(state.notifications.length).to.eql(1) + expect(state.notifications[0].status).to.eql(mentionedStatus) + expect(state.notifications[0].action).to.eql(mentionedStatus) + expect(state.notifications[0].type).to.eql('mention') + + mutations.addNewStatuses(state, { statuses: [deletion], user }) + expect(state.allStatuses.length).to.eql(1) + expect(state.notifications.length).to.eql(0) + }) + it('adds the message to mentions when you are mentioned', () => { const user = { id: 1 } const state = cloneDeep(defaultState) -- cgit v1.2.3-70-g09d2 From e663420260bc135f0851a996a67a87a947580bf4 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 13 Jun 2017 12:01:47 +0200 Subject: Fix notification deletion. --- src/modules/statuses.js | 2 +- test/unit/specs/modules/statuses.spec.js | 33 +++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) (limited to 'test') diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 0b4069c6..c3753c5a 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -259,7 +259,7 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us return } - remove(state.notifications, ({action: {id}}) => status.id) + remove(state.notifications, ({action: {id}}) => id === status.id) remove(allStatuses, { uri }) if (timeline) { diff --git a/test/unit/specs/modules/statuses.spec.js b/test/unit/specs/modules/statuses.spec.js index 66d6cfd7..d25cc108 100644 --- a/test/unit/specs/modules/statuses.spec.js +++ b/test/unit/specs/modules/statuses.spec.js @@ -125,18 +125,19 @@ describe('The Statuses module', () => { it('removes statuses by tag on deletion', () => { const state = cloneDeep(defaultState) const status = makeMockStatus({id: 1}) + const otherStatus = makeMockStatus({id: 3}) status.uri = 'xxx' const deletion = makeMockStatus({id: 2, is_post_verb: false}) deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.' deletion.uri = 'xxx' - mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) + mutations.addNewStatuses(state, { statuses: [status, otherStatus], showImmediately: true, timeline: 'public' }) mutations.addNewStatuses(state, { statuses: [deletion], showImmediately: true, timeline: 'public' }) - expect(state.allStatuses).to.eql([]) - expect(state.timelines.public.statuses).to.eql([]) - expect(state.timelines.public.visibleStatuses).to.eql([]) - expect(state.timelines.public.maxId).to.eql(2) + expect(state.allStatuses).to.eql([otherStatus]) + expect(state.timelines.public.statuses).to.eql([otherStatus]) + expect(state.timelines.public.visibleStatuses).to.eql([otherStatus]) + expect(state.timelines.public.maxId).to.eql(3) }) it('does not update the maxId when the noIdUpdate flag is set', () => { @@ -323,28 +324,30 @@ describe('The Statuses module', () => { const user = { id: 1 } const state = cloneDeep(defaultState) const status = makeMockStatus({id: 1}) + const otherStatus = makeMockStatus({id: 3}) const mentionedStatus = makeMockStatus({id: 2}) mentionedStatus.attentions = [user] mentionedStatus.uri = 'xxx' + otherStatus.attentions = [user] - const deletion = makeMockStatus({id: 3, is_post_verb: false}) + const deletion = makeMockStatus({id: 4, is_post_verb: false}) deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.' deletion.uri = 'xxx' - mutations.addNewStatuses(state, { statuses: [status], user }) + mutations.addNewStatuses(state, { statuses: [status, otherStatus], user }) - expect(state.notifications.length).to.eql(0) + expect(state.notifications.length).to.eql(1) mutations.addNewStatuses(state, { statuses: [mentionedStatus], user }) - expect(state.allStatuses.length).to.eql(2) - expect(state.notifications.length).to.eql(1) - expect(state.notifications[0].status).to.eql(mentionedStatus) - expect(state.notifications[0].action).to.eql(mentionedStatus) - expect(state.notifications[0].type).to.eql('mention') + expect(state.allStatuses.length).to.eql(3) + expect(state.notifications.length).to.eql(2) + expect(state.notifications[1].status).to.eql(mentionedStatus) + expect(state.notifications[1].action).to.eql(mentionedStatus) + expect(state.notifications[1].type).to.eql('mention') mutations.addNewStatuses(state, { statuses: [deletion], user }) - expect(state.allStatuses.length).to.eql(1) - expect(state.notifications.length).to.eql(0) + expect(state.allStatuses.length).to.eql(2) + expect(state.notifications.length).to.eql(1) }) it('adds the message to mentions when you are mentioned', () => { -- cgit v1.2.3-70-g09d2