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

Commit

Permalink
Merge pull request #1268 from atom/wl-repo-for-path-specs
Browse files Browse the repository at this point in the history
Add specs for repoForPath
  • Loading branch information
Winston Liu authored Jul 5, 2018
2 parents 48b7cae + bbf28b7 commit aab456b
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
103 changes: 103 additions & 0 deletions spec/async-spec-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/** @babel */

export function beforeEach (fn) {
global.beforeEach(function () {
const result = fn()
if (result instanceof Promise) {
waitsForPromise(() => result)
}
})
}

export function afterEach (fn) {
global.afterEach(function () {
const result = fn()
if (result instanceof Promise) {
waitsForPromise(() => result)
}
})
}

['it', 'fit', 'ffit', 'fffit'].forEach(function (name) {
module.exports[name] = function (description, fn) {
if (fn === undefined) {
global[name](description)
return
}

global[name](description, function () {
const result = fn()
if (result instanceof Promise) {
waitsForPromise(() => result)
}
})
}
})

export async function conditionPromise (condition, description = 'anonymous condition') {
const startTime = Date.now()

while (true) {
await timeoutPromise(100)

if (await condition()) {
return
}

if (Date.now() - startTime > 5000) {
throw new Error('Timed out waiting on ' + description)
}
}
}

export function timeoutPromise (timeout) {
return new Promise(function (resolve) {
global.setTimeout(resolve, timeout)
})
}

function waitsForPromise (fn) {
const promise = fn()
global.waitsFor('spec promise to resolve', function (done) {
promise.then(done, function (error) {
jasmine.getEnv().currentSpec.fail(error)
done()
})
})
}

export function emitterEventPromise (emitter, event, timeout = 15000) {
return new Promise((resolve, reject) => {
const timeoutHandle = setTimeout(() => {
reject(new Error(`Timed out waiting for '${event}' event`))
}, timeout)
emitter.once(event, () => {
clearTimeout(timeoutHandle)
resolve()
})
})
}

export function promisify (original) {
return function (...args) {
return new Promise((resolve, reject) => {
args.push((err, ...results) => {
if (err) {
reject(err)
} else {
resolve(...results)
}
})

return original(...args)
})
}
}

export function promisifySome (obj, fnNames) {
const result = {}
for (const fnName of fnNames) {
result[fnName] = promisify(obj[fnName])
}
return result
}
23 changes: 23 additions & 0 deletions spec/helpers-spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
const {it, fit, ffit, beforeEach, afterEach} = require('./async-spec-helpers') // eslint-disable-line no-unused-vars

const path = require('path')

const helpers = require('../lib/helpers')

describe('Helpers', () => {
describe('repoForPath', () => {
let fixturesPath, fixturesRepo

beforeEach(async () => {
fixturesPath = atom.project.getPaths()[0]
fixturesRepo = await atom.project.repositoryForDirectory(atom.project.getDirectories()[0])
})

it('returns the repository for a given project path', () => {
expect(helpers.repoForPath(fixturesPath)).toEqual(fixturesRepo)
})

it('returns the project repository for a subpath', () => {
expect(helpers.repoForPath(path.join(fixturesPath, 'root-dir1', 'tree-view.txt'))).toEqual(fixturesRepo)
})

it('returns null for a path outside the project', () => {
expect(helpers.repoForPath(path.join(fixturesPath, '..'))).toEqual(null)
})
})

describe('getFullExtension', () => {
it('returns the extension for a simple file', () => {
expect(helpers.getFullExtension('filename.txt')).toBe('.txt')
Expand Down

0 comments on commit aab456b

Please sign in to comment.