Skip to content

Commit

Permalink
Merge pull request #45 from onigoetz/master
Browse files Browse the repository at this point in the history
Do not fail when browserslist gives a browser that caniuse-api doesn't know about
  • Loading branch information
MoOx authored Aug 6, 2016
2 parents dad8ced + 647ba7e commit 425a227
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function isSupported(feature, browsers) {

return browserslist(browsers)
.map((browser) => browser.split(" "))
.every((browser) => data.stats[browser[0]][browser[1]] === "y")
.every((browser) => data.stats[browser[0]] && data.stats[browser[0]][browser[1]] === "y")
}

function find(query) {
Expand Down
22 changes: 20 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,31 @@ test("isSupported tests", (t) => {
t.end()
})

// If for some reason the caniuse-db is not the same in browserslist and in caniuse-api
// browserslist could return browsers that caniuse-api doesn't know about and crashes
test("isSupported test with browsers caniuse doesn't know", (t) => {
browserslist.data.notabrowser = {
name: 'notabrowser',
versions: ['1'],
released: ['1']
};
browserslist.versionAliases.notabrowser = {}

t.notOk(caniuse.isSupported("border-radius", "notabrowser 1"), "do not throw on non existing data")

delete browserslist.data.notabrowser
delete browserslist.versionAliases.notabrowser
t.end()
})

test("getSupport tests", (t) => {
caniuse.setBrowserScope()

let borderRadiusFeature = require('caniuse-db/features-json/border-radius')
let borderRadiusSupport = {
safari: { y: 3.1, x: 4, '#1': 6.1 },
opera: { n: 10, y: 10.5 },
op_mini: { n: 5 },
op_mini: {},
ios_saf: { y: 3.2, x: 3.2 },
ie_mob: { y: 10 },
edge: { y: 12 },
Expand All @@ -51,7 +68,8 @@ test("getSupport tests", (t) => {
android: { y: 2.1, x: 2.1 },
and_uc: { y: 9.9 },
// workaround for https://github.com/Nyalab/caniuse-api/issues/19
and_chr: { y: parseInt(Object.keys(borderRadiusFeature.stats.and_chr).filter(v => borderRadiusFeature.stats.and_chr[v] === "y").pop()) }
and_chr: { y: parseInt(Object.keys(borderRadiusFeature.stats.and_chr).filter(v => borderRadiusFeature.stats.and_chr[v] === "y").pop()) },
samsung: { y: 4 }
}

t.deepEqual(caniuse.getSupport("border-radius"), borderRadiusSupport, "border-radius support is ok")
Expand Down
5 changes: 3 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test("parseCaniuseData should work", (t) => {
let correctSupport = {
safari: { y: 3.1, x: 4, '#1': 6.1 },
opera: { n: 10, y: 10.5 },
op_mini: { n: 5 },
op_mini: { },
ios_saf: { y: 3.2, x: 3.2 },
ie_mob: { y: 10 },
ie: { n: 8, y: 9 },
Expand All @@ -27,7 +27,8 @@ test("parseCaniuseData should work", (t) => {
android: { y: 2.1, x: 2.1 },
and_uc: { y: 9.9 },
// workaround for https://github.com/Nyalab/caniuse-api/issues/19
and_chr: { y: parseInt(Object.keys(borderRadiusFeature.stats.and_chr).filter(v => borderRadiusFeature.stats.and_chr[v] === "y").pop()) }
and_chr: { y: parseInt(Object.keys(borderRadiusFeature.stats.and_chr).filter(v => borderRadiusFeature.stats.and_chr[v] === "y").pop()) },
samsung: { y: 4 }
}

t.deepEqual(parseCaniuseData(borderRadiusFeature, browsers), correctSupport, "border-radius support is correct")
Expand Down

0 comments on commit 425a227

Please sign in to comment.