Enhance service
This commit is contained in:
16
test/data.js
16
test/data.js
@@ -1,3 +1,19 @@
|
||||
export const remoteServices = {
|
||||
'github-pr': {
|
||||
name: 'github',
|
||||
urlPattern: 'https://github.com/:org/:repo/pull/:id',
|
||||
id: (document, service, { org, repo, id }) => [org, repo, service.key, id].join('-'),
|
||||
description: 'This is always the same text',
|
||||
projectId: (document) => {
|
||||
const match = document.querySelector(".gh-header-title").textContent.trim().match(/\[(\d+)\]/)
|
||||
return match && match[1]
|
||||
}
|
||||
},
|
||||
'jira-cloud': {
|
||||
name: 'jira',
|
||||
urlPattern: 'https://cloud.jira.com/browse?project=:project&issue=:id'
|
||||
}
|
||||
}
|
||||
export const projects = [
|
||||
{
|
||||
id: 944868981,
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import DomainCheck from '../../src/js/services/DomainCheck'
|
||||
|
||||
describe('services', () => {
|
||||
describe('DomainCheck', () => {
|
||||
const config = {
|
||||
services: [
|
||||
{
|
||||
name: 'github',
|
||||
urlPattern: 'https://github.com/:org/:repo/pull/:id',
|
||||
},
|
||||
{
|
||||
name: 'jira',
|
||||
urlPattern: 'https://support.jira.com/browse?project=:project&issue=:id'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
let domainCheck
|
||||
|
||||
beforeAll(() => {
|
||||
domainCheck = new DomainCheck(config)
|
||||
})
|
||||
|
||||
it('matches host and path', () => {
|
||||
const service = domainCheck.match('https://github.com/hundertzehn/mocoapp/pull/123')
|
||||
expect(service.name).toEqual('github')
|
||||
expect(service.match).toEqual({
|
||||
org: 'hundertzehn',
|
||||
repo: 'mocoapp',
|
||||
id: '123',
|
||||
})
|
||||
})
|
||||
|
||||
it('matches query string', () => {
|
||||
const service = domainCheck.match('https://support.jira.com/browse?project=mocoapp&issue=1234')
|
||||
expect(service.name).toEqual('jira')
|
||||
expect(service.match).toEqual({
|
||||
project: 'mocoapp',
|
||||
id: '1234',
|
||||
})
|
||||
})
|
||||
|
||||
it('does not match different host', () => {
|
||||
const service = domainCheck.match('https://trello.com/hundertzehn/mocoapp/pull/123')
|
||||
expect(service).toBeFalsy()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,9 +1,9 @@
|
||||
import { projects } from "./data"
|
||||
import { projects } from "../data"
|
||||
import {
|
||||
findLastProject,
|
||||
findLastTask,
|
||||
groupedProjectOptions
|
||||
} from "../src/js/utils"
|
||||
} from "../../src/js/utils"
|
||||
import { map } from "lodash/fp"
|
||||
|
||||
describe("utils", () => {
|
||||
64
test/utils/urlMatcher.test.js
Normal file
64
test/utils/urlMatcher.test.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import { remoteServices } from '../data'
|
||||
import { parseServices, createMatcher, createEnhancer } from '../../src/js/utils/urlMatcher'
|
||||
import Route from "route-parser"
|
||||
|
||||
describe('utils', () => {
|
||||
describe("urlMatcher", () => {
|
||||
describe("parseServices", () => {
|
||||
it("parses the services", () => {
|
||||
const services = parseServices(remoteServices)
|
||||
|
||||
let service = services[0]
|
||||
expect(service.key).toEqual("github-pr")
|
||||
expect(service.name).toEqual("github")
|
||||
expect(service.route).toBeInstanceOf(Route)
|
||||
|
||||
service = services[1]
|
||||
expect(service.key).toEqual("jira-cloud")
|
||||
expect(service.name).toEqual("jira")
|
||||
expect(service.route).toBeInstanceOf(Route)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createMatcher", () => {
|
||||
let services, matcher
|
||||
|
||||
beforeEach(() => {
|
||||
services = parseServices(remoteServices)
|
||||
matcher = createMatcher(services)
|
||||
})
|
||||
|
||||
it('matches host and path', () => {
|
||||
const service = matcher('https://github.com/hundertzehn/mocoapp/pull/123')
|
||||
expect(service.key).toEqual('github-pr')
|
||||
expect(service.name).toEqual('github')
|
||||
})
|
||||
|
||||
it('matches query string', () => {
|
||||
const service = matcher('https://cloud.jira.com/browse?project=mocoapp&issue=1234')
|
||||
expect(service.key).toEqual('jira-cloud')
|
||||
expect(service.name).toEqual('jira')
|
||||
})
|
||||
|
||||
it('does not match different host', () => {
|
||||
const service = matcher('https://trello.com/hundertzehn/mocoapp/pull/123')
|
||||
expect(service).toBeFalsy()
|
||||
})
|
||||
})
|
||||
|
||||
describe("createEnhancer", () => {
|
||||
it("enhances a services", () => {
|
||||
const url = 'https://github.com/hundertzehn/mocoapp/pull/123'
|
||||
const document = {
|
||||
querySelector: jest.fn().mockReturnValue({ textContent: '[4321] Foo' })
|
||||
}
|
||||
const enhancedService = createEnhancer(document)(remoteServices)('github-pr', url)
|
||||
expect(enhancedService.id).toEqual( 'hundertzehn-mocoapp-github-pr-123')
|
||||
expect(enhancedService.description).toEqual('This is always the same text')
|
||||
expect(enhancedService.projectId).toEqual('4321')
|
||||
expect(enhancedService.taskId).toBe(undefined)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user