aboutsummaryrefslogtreecommitdiff
path: root/test/unit/specs/services
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/specs/services')
-rw-r--r--test/unit/specs/services/chat_service/chat_service.spec.js88
-rw-r--r--test/unit/specs/services/completion/completion.spec.js43
-rw-r--r--test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js22
3 files changed, 138 insertions, 15 deletions
diff --git a/test/unit/specs/services/chat_service/chat_service.spec.js b/test/unit/specs/services/chat_service/chat_service.spec.js
new file mode 100644
index 00000000..3ee9839d
--- /dev/null
+++ b/test/unit/specs/services/chat_service/chat_service.spec.js
@@ -0,0 +1,88 @@
+import chatService from '../../../../../src/services/chat_service/chat_service.js'
+
+const message1 = {
+ id: '9wLkdcmQXD21Oy8lEX',
+ created_at: (new Date('2020-06-22T18:45:53.000Z'))
+}
+
+const message2 = {
+ id: '9wLkdp6ihaOVdNj8Wu',
+ account_id: '9vmRb29zLQReckr5ay',
+ created_at: (new Date('2020-06-22T18:45:56.000Z'))
+}
+
+const message3 = {
+ id: '9wLke9zL4Dy4OZR2RM',
+ account_id: '9vmRb29zLQReckr5ay',
+ created_at: (new Date('2020-07-22T18:45:59.000Z'))
+}
+
+describe('chatService', () => {
+ describe('.add', () => {
+ it("Doesn't add duplicates", () => {
+ const chat = chatService.empty()
+ chatService.add(chat, { messages: [ message1 ] })
+ chatService.add(chat, { messages: [ message1 ] })
+ expect(chat.messages.length).to.eql(1)
+
+ chatService.add(chat, { messages: [ message2 ] })
+ expect(chat.messages.length).to.eql(2)
+ })
+
+ it('Updates minId and lastMessage and newMessageCount', () => {
+ const chat = chatService.empty()
+
+ chatService.add(chat, { messages: [ message1 ] })
+ expect(chat.lastMessage.id).to.eql(message1.id)
+ expect(chat.minId).to.eql(message1.id)
+ expect(chat.newMessageCount).to.eql(1)
+
+ chatService.add(chat, { messages: [ message2 ] })
+ expect(chat.lastMessage.id).to.eql(message2.id)
+ expect(chat.minId).to.eql(message1.id)
+ expect(chat.newMessageCount).to.eql(2)
+
+ chatService.resetNewMessageCount(chat)
+ expect(chat.newMessageCount).to.eql(0)
+
+ const createdAt = new Date()
+ createdAt.setSeconds(createdAt.getSeconds() + 10)
+ chatService.add(chat, { messages: [ { message3, created_at: createdAt } ] })
+ expect(chat.newMessageCount).to.eql(1)
+ })
+ })
+
+ describe('.delete', () => {
+ it('Updates minId and lastMessage', () => {
+ const chat = chatService.empty()
+
+ chatService.add(chat, { messages: [ message1 ] })
+ chatService.add(chat, { messages: [ message2 ] })
+ chatService.add(chat, { messages: [ message3 ] })
+
+ expect(chat.lastMessage.id).to.eql(message3.id)
+ expect(chat.minId).to.eql(message1.id)
+
+ chatService.deleteMessage(chat, message3.id)
+ expect(chat.lastMessage.id).to.eql(message2.id)
+ expect(chat.minId).to.eql(message1.id)
+
+ chatService.deleteMessage(chat, message1.id)
+ expect(chat.lastMessage.id).to.eql(message2.id)
+ expect(chat.minId).to.eql(message2.id)
+ })
+ })
+
+ describe('.getView', () => {
+ it('Inserts date separators', () => {
+ const chat = chatService.empty()
+
+ chatService.add(chat, { messages: [ message1 ] })
+ chatService.add(chat, { messages: [ message2 ] })
+ chatService.add(chat, { messages: [ message3 ] })
+
+ const view = chatService.getView(chat)
+ expect(view.map(i => i.type)).to.eql(['date', 'message', 'message', 'date', 'message'])
+ })
+ })
+})
diff --git a/test/unit/specs/services/completion/completion.spec.js b/test/unit/specs/services/completion/completion.spec.js
index 8a41c653..81d3a26a 100644
--- a/test/unit/specs/services/completion/completion.spec.js
+++ b/test/unit/specs/services/completion/completion.spec.js
@@ -1,8 +1,8 @@
-import { replaceWord, addPositionToWords, wordAtPosition, splitIntoWords } from '../../../../../src/services/completion/completion.js'
+import { replaceWord, addPositionToWords, wordAtPosition, splitByWhitespaceBoundary } from '../../../../../src/services/completion/completion.js'
describe('addPositiontoWords', () => {
it('adds the position to a word list', () => {
- const words = ['hey', 'this', 'is', 'fun']
+ const words = ['hey', ' ', 'this', ' ', 'is', ' ', 'fun']
const expected = [
{
@@ -11,19 +11,34 @@ describe('addPositiontoWords', () => {
end: 3
},
{
- word: 'this',
+ word: ' ',
start: 3,
- end: 7
+ end: 4
},
{
- word: 'is',
- start: 7,
+ word: 'this',
+ start: 4,
+ end: 8
+ },
+ {
+ word: ' ',
+ start: 8,
end: 9
},
{
- word: 'fun',
+ word: 'is',
start: 9,
+ end: 11
+ },
+ {
+ word: ' ',
+ start: 11,
end: 12
+ },
+ {
+ word: 'fun',
+ start: 12,
+ end: 15
}
]
@@ -33,11 +48,11 @@ describe('addPositiontoWords', () => {
})
})
-describe('splitIntoWords', () => {
+describe('splitByWhitespaceBoundary', () => {
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)
+ const str = 'This is a #nice @test for you, @idiot@idiot.com'
+ const expected = ['This', ' ', 'is', ' ', 'a', ' ', '#nice', ' ', '@test', ' ', 'for', ' ', 'you,', ' ', '@idiot@idiot.com']
+ const res = splitByWhitespaceBoundary(str)
expect(res).to.eql(expected)
})
@@ -57,13 +72,13 @@ describe('wordAtPosition', () => {
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 str = 'hey @take , how are you'
+ const wordsWithPosition = addPositionToWords(splitByWhitespaceBoundary(str))
const toReplace = wordsWithPosition[2]
expect(toReplace.word).to.eql('@take')
- const expected = 'hey @takeshitakenji, how are you'
+ const expected = 'hey @takeshitakenji , how are you'
const res = replaceWord(str, toReplace, '@takeshitakenji')
expect(res).to.eql(expected)
})
diff --git a/test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js b/test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js
index ccb57942..e1f7a958 100644
--- a/test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js
+++ b/test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js
@@ -1,4 +1,4 @@
-import { parseStatus, parseUser, parseNotification, addEmojis } from '../../../../../src/services/entity_normalizer/entity_normalizer.service.js'
+import { parseStatus, parseUser, parseNotification, addEmojis, parseLinkHeaderPagination } from '../../../../../src/services/entity_normalizer/entity_normalizer.service.js'
import mastoapidata from '../../../../fixtures/mastoapi.json'
import qvitterapidata from '../../../../fixtures/statuses.json'
@@ -383,4 +383,24 @@ describe('API Entities normalizer', () => {
expect(result).to.include('title=\':[a-z] {|}*:\'')
})
})
+
+ describe('Link header pagination', () => {
+ it('Parses min and max ids as integers', () => {
+ const linkHeader = '<https://example.com/api/v1/notifications?max_id=861676>; rel="next", <https://example.com/api/v1/notifications?min_id=861741>; rel="prev"'
+ const result = parseLinkHeaderPagination(linkHeader)
+ expect(result).to.eql({
+ 'maxId': 861676,
+ 'minId': 861741
+ })
+ })
+
+ it('Parses min and max ids as flakes', () => {
+ const linkHeader = '<http://example.com/api/v1/timelines/home?max_id=9waQx5IIS48qVue2Ai>; rel="next", <http://example.com/api/v1/timelines/home?min_id=9wi61nIPnfn674xgie>; rel="prev"'
+ const result = parseLinkHeaderPagination(linkHeader, { flakeId: true })
+ expect(result).to.eql({
+ 'maxId': '9waQx5IIS48qVue2Ai',
+ 'minId': '9wi61nIPnfn674xgie'
+ })
+ })
+ })
})