Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
test: isBrowser()
Browse files Browse the repository at this point in the history
  • Loading branch information
totorogendut committed Apr 17, 2020
1 parent 77e18a3 commit 83fbeef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
16 changes: 16 additions & 0 deletions src/fund/main-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ export function clientSideFund(pointer?: WMAddress, options: fundOptions = {}):
}
throw FundmeError(invalidAddress)
}

let forceBrowser: boolean = false
export function forceFundmeOnBrowser() {
forceBrowser = true
}

export const isBrowser = (options: fundOptions = {}): boolean => {
if (options.force === 'server') return false

if (forceBrowser || options.force === 'client') {
forceBrowser = false
return true
}

return require === undefined && module === undefined
}
17 changes: 3 additions & 14 deletions src/fund/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getCurrentPointerAddressMustClientSide,
FundmeError,
} from './errors'
import { clientSideFund } from './main-client'
import { clientSideFund, isBrowser, forceFundmeOnBrowser } from './main-client'
import { serverSideFund } from './main-server'

export let defaultAddress: WMAddress
Expand All @@ -33,19 +33,6 @@ export function fund(pointer?: WMAddress, options: fundOptions = {}): FundType |
}
}

let forceBrowser: boolean = false
export function forceFundmeOnBrowser() {
forceBrowser = true
}

export const isBrowser = (options: fundOptions = {}): boolean => {
if (forceBrowser || options.force === 'client') {
forceBrowser = false
return true
}
return require === undefined && module === undefined
}

export function setDefaultAddress(address: WMAddress): void {
if (Array.isArray(address)) {
defaultAddress = createPool(address)
Expand Down Expand Up @@ -96,3 +83,5 @@ export function convertToPointerPool(pointer: WMAddress): Array<string | WMPoint

return pointer
}

export { isBrowser, forceFundmeOnBrowser }
4 changes: 2 additions & 2 deletions src/fund/set-pointer-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const FUNDME_JSON_SELECTOR = 'script[fundme]'

type JSONTemplate = Array<WMPointer | string>

export function setPointerFromTemplates(): void {
export function setPointerFromTemplates(options: fundOptions = {}): void {
const pointers: Array<WMPointer> = [...scrapeTemplate(), ...scrapeJson(), ...scrapeCustomSyntax()]

if (pointers.length) {
setPointerMultiple(pointers)
setPointerMultiple(pointers, options)
} else {
throw FundmeError(noTemplateFound)
}
Expand Down
14 changes: 13 additions & 1 deletion tests/fund/client-side/fundme-force-browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fund } from '../../../src/fund/main'
import { fund, isBrowser } from '../../../src/fund/main'

import { toBeInTheDocument, toHaveAttribute } from '@testing-library/jest-dom/matchers'

Expand Down Expand Up @@ -31,3 +31,15 @@ describe('force fund() on browser', () => {
expect(meta.content).toContain('$wallet.example.com/test')
})
})

describe('test isBrowser()', () => {
test('force client is enabled', () => {
expect(isBrowser({ force: 'client' })).toBeTruthy()
})
test('force server is enabled', () => {
expect(isBrowser({ force: 'server' })).toBeFalsy()
})
test('without forcing in node environment', () => {
expect(isBrowser()).toBeFalsy()
})
})

0 comments on commit 83fbeef

Please sign in to comment.