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

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jan 15, 2017
1 parent 4a0d019 commit 33d2e50
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 5 deletions.
12 changes: 9 additions & 3 deletions app/browser/bookmarksExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const isWindows = process.platform === 'win32'
const indentLength = 2
const indentType = ' '

module.exports.dialog = (sites) => {
function showDialog (sites) {
const focusedWindow = BrowserWindow.getFocusedWindow()
const fileName = moment().format('DD_MM_YYYY') + '.html'
const defaultPath = path.join(getSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH) || app.getPath('downloads'), fileName)
Expand All @@ -36,7 +36,7 @@ module.exports.dialog = (sites) => {
if (fileName) {
personal = createBookmarkArray(sites)
other = createBookmarkArray(sites, -1, false)
fs.writeFileSync(fileName, createStringHTML(personal, other))
fs.writeFileSync(fileName, createBookmarkHTML(personal, other))
}
})
}
Expand Down Expand Up @@ -73,7 +73,7 @@ function createBookmarkArray (sites, parentFolderId, first = true, depth = 1) {
return payload
}

function createStringHTML (personal, other) {
function createBookmarkHTML (personal, other) {
const breakTag = (isWindows) ? '\r\n' : '\n'
const title = 'Bookmarks'

Expand All @@ -88,3 +88,9 @@ ${personal.join(breakTag)}
${other.join(breakTag)}
</DL><p>`
}

module.exports = {
createBookmarkArray,
createBookmarkHTML,
showDialog
}
4 changes: 2 additions & 2 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ app.on('ready', () => {
})

ipcMain.on(messages.EXPORT_BOOKMARKS, () => {
BookmarksExporter.dialog(AppStore.getState().get('sites'))
BookmarksExporter.showDialog(AppStore.getState().get('sites'))
})

// This loads package.json into an object
Expand All @@ -751,7 +751,7 @@ app.on('ready', () => {
})

process.on(messages.EXPORT_BOOKMARKS, () => {
BookmarksExporter.dialog(AppStore.getState().get('sites'))
BookmarksExporter.showDialog(AppStore.getState().get('sites'))
})
})
ready = true
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/bookmarkExport.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file. It will be read and overwritten. DO NOT EDIT! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><H3 PERSONAL_TOOLBAR_FOLDER="true">Bookmarks Bar</H3>
<DL><p>
<DT><A HREF="https://brave.com/1">Website 1</A>
<DT><H3>folder 1</H3>
<DL><p>
<DT><A HREF="https://brave.com/2">Website 2</A>
<DT><H3>folder 2</H3>
<DL><p>
<DT><A HREF="https://brave.com/3">Website 3</A>
<DT><A HREF="https://brave.com/4">Website 4</A>
</DL><p>
<DT><H3>folder 3</H3>
<DL><p>
<DT><A HREF="https://brave.com/5">Website 5</A>
</DL><p>
</DL><p>
<DT><A HREF="https://brave.com/6">Website 6</A>
</DL><p>
<DT><A HREF="https://brave.com/7">Website 7</A>
<DT><H3>folder 4</H3>
<DL><p>
<DT><A HREF="https://brave.com/8">Website 8</A>
<DT><A HREF="https://brave.com/9">Website 9</A>
</DL><p>
</DL><p>
109 changes: 109 additions & 0 deletions test/unit/app/browser/exportBookmarksTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* global describe, it, before, after */
const assert = require('assert')
const mockery = require('mockery')
const fs = require('fs')
const Immutable = require('immutable')

const siteTags = require('../../../../js/constants/siteTags')
require('../../braveUnit')

describe('Bookmarks export', function () {
let exporter

before(function () {
mockery.enable({
warnOnReplace: false,
warnOnUnregistered: false,
useCleanCache: true
})
mockery.registerMock('electron', require('../../lib/fakeElectron'))
exporter = require('../../../../app/browser/bookmarksExporter')
})

after(function () {
mockery.disable()
})

/**
* Toolbar
* + website 1
* + folder 1
* ++ website 2
* ++ folder 2
* +++ website 3
* +++ website 4
* ++ folder 3
* +++ website 5
* +website 6
*
* Other
* + website 7
* + folder 4
* ++ website 8
* ++ website 9
*/
const sites = Immutable.fromJS([
{ tags: [], title: 'Fake', location: 'https://brave.com' },
{ tags: [siteTags.BOOKMARK], title: 'Website 1', location: 'https://brave.com/1' },
{ tags: [siteTags.BOOKMARK_FOLDER], title: 'folder 1', folderId: 1 },
{ tags: [siteTags.BOOKMARK], title: 'Website 2', location: 'https://brave.com/2', parentFolderId: 1 },
{ tags: [siteTags.BOOKMARK_FOLDER], title: 'folder 2', folderId: 2, parentFolderId: 1 },
{ tags: [siteTags.BOOKMARK], title: 'Website 3', location: 'https://brave.com/3', parentFolderId: 2 },
{ tags: [siteTags.BOOKMARK], title: 'Website 4', location: 'https://brave.com/4', parentFolderId: 2 },
{ tags: [siteTags.BOOKMARK_FOLDER], title: 'folder 3', folderId: 3, parentFolderId: 1 },
{ tags: [siteTags.BOOKMARK], title: 'Website 5', location: 'https://brave.com/5', parentFolderId: 3 },
{ tags: [siteTags.BOOKMARK], title: 'Website 6', location: 'https://brave.com/6' },
{ tags: [siteTags.BOOKMARK], title: 'Website 7', location: 'https://brave.com/7', parentFolderId: -1 },
{ tags: [siteTags.BOOKMARK_FOLDER], title: 'folder 4', folderId: 4, parentFolderId: -1 },
{ tags: [siteTags.BOOKMARK], title: 'Website 8', location: 'https://brave.com/8', parentFolderId: 4 },
{ tags: [siteTags.BOOKMARK], title: 'Website 9', location: 'https://brave.com/9', parentFolderId: 4 }
])

const personalArray = [
' <DL><p>',
' <DT><A HREF="https://brave.com/1">Website 1</A>',
' <DT><H3>folder 1</H3>',
' <DL><p>',
' <DT><A HREF="https://brave.com/2">Website 2</A>',
' <DT><H3>folder 2</H3>',
' <DL><p>',
' <DT><A HREF="https://brave.com/3">Website 3</A>',
' <DT><A HREF="https://brave.com/4">Website 4</A>',
' </DL><p>',
' <DT><H3>folder 3</H3>',
' <DL><p>',
' <DT><A HREF="https://brave.com/5">Website 5</A>',
' </DL><p>',
' </DL><p>',
' <DT><A HREF="https://brave.com/6">Website 6</A>',
' </DL><p>'
]

const otherArray = [
' <DT><A HREF="https://brave.com/7">Website 7</A>',
' <DT><H3>folder 4</H3>',
' <DL><p>',
' <DT><A HREF="https://brave.com/8">Website 8</A>',
' <DT><A HREF="https://brave.com/9">Website 9</A>',
' </DL><p>'
]

it('personal array', function () {
const gen = exporter.createBookmarkArray(sites)
assert.deepEqual(gen, personalArray)
})

it('other array', function () {
const gen = exporter.createBookmarkArray(sites, -1, false)
assert.deepEqual(gen, otherArray)
})

it('generated html', function () {
const personal = exporter.createBookmarkArray(sites)
const other = exporter.createBookmarkArray(sites, -1, false)
const result = exporter.createBookmarkHTML(personal, other)
const expected = fs.readFileSync('./test/fixtures/bookmarkExport.html', 'utf8')

assert.equal(result, expected)
})
})

0 comments on commit 33d2e50

Please sign in to comment.