From 835c2e9c361b9f13bd7062aefc4b59fa15a10a14 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 29 Dec 2019 15:24:18 -0800 Subject: [PATCH] fix: remove rAF polyfill, add SSR test --- .travis.yml | 1 + package.json | 5 ++-- src/components/picker/nimble-picker.js | 8 ++---- src/vendor/raf-polyfill.js | 39 -------------------------- test/ssr.js | 26 +++++++++++++++++ 5 files changed, 33 insertions(+), 46 deletions(-) delete mode 100644 src/vendor/raf-polyfill.js create mode 100644 test/ssr.js diff --git a/.travis.yml b/.travis.yml index aa6a7a8..d53bf4b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ script: yarn test before_script: - yarn prettier:check - yarn test:size +- yarn test:ssr branches: only: - master diff --git a/package.json b/package.json index 5bd3361..b2e236a 100644 --- a/package.json +++ b/package.json @@ -77,11 +77,12 @@ "react:15": "npm run react:clean && npm i react@^15 react-dom@^15 react-addons-test-utils@^15 --save-dev", "test": "npm run clean && jest", "test:size": "size-limit", + "test:ssr": "node test/ssr.js", "prepublishOnly": "npm run build", "storybook": "start-storybook -p 6006", "build-storybook": "build-storybook", - "prettier": "prettier --write \"{src,scripts}/**/*.js\"", - "prettier:check": "prettier --check \"{src,scripts}/**/*.js\"", + "prettier": "prettier --write \"{src,scripts,test}/**/*.js\"", + "prettier:check": "prettier --check \"{src,scripts,test}/**/*.js\"", "prepare": "npm run build:dist" }, "size-limit": [ diff --git a/src/components/picker/nimble-picker.js b/src/components/picker/nimble-picker.js index 3c1e8b4..631f091 100644 --- a/src/components/picker/nimble-picker.js +++ b/src/components/picker/nimble-picker.js @@ -1,5 +1,3 @@ -import '../../vendor/raf-polyfill' - import React from 'react' import PropTypes from 'prop-types' @@ -293,7 +291,7 @@ export default class NimblePicker extends React.PureComponent { let maxMargin = component.maxMargin component.forceUpdate() - window.requestAnimationFrame(() => { + requestAnimationFrame(() => { if (!this.scroll) return component.memoizeSize() if (maxMargin == component.maxMargin) return @@ -311,7 +309,7 @@ export default class NimblePicker extends React.PureComponent { handleScroll() { if (!this.waitingForPaint) { this.waitingForPaint = true - window.requestAnimationFrame(this.handleScrollPaint) + requestAnimationFrame(this.handleScrollPaint) } } @@ -415,7 +413,7 @@ export default class NimblePicker extends React.PureComponent { this.handleSearch(null) this.search.clear() - window.requestAnimationFrame(scrollToComponent) + requestAnimationFrame(scrollToComponent) } else { scrollToComponent() } diff --git a/src/vendor/raf-polyfill.js b/src/vendor/raf-polyfill.js deleted file mode 100644 index 8bf3225..0000000 --- a/src/vendor/raf-polyfill.js +++ /dev/null @@ -1,39 +0,0 @@ -// http://paulirish.com/2011/requestanimationframe-for-smart-animating/ -// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating - -// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel - -// MIT license - -var isWindowAvailable = typeof window !== 'undefined' - -isWindowAvailable && - (function() { - var lastTime = 0 - var vendors = ['ms', 'moz', 'webkit', 'o'] - - for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { - window.requestAnimationFrame = - window[vendors[x] + 'RequestAnimationFrame'] - window.cancelAnimationFrame = - window[vendors[x] + 'CancelAnimationFrame'] || - window[vendors[x] + 'CancelRequestAnimationFrame'] - } - - if (!window.requestAnimationFrame) - window.requestAnimationFrame = function(callback, element) { - var currTime = new Date().getTime() - var timeToCall = Math.max(0, 16 - (currTime - lastTime)) - var id = window.setTimeout(function() { - callback(currTime + timeToCall) - }, timeToCall) - - lastTime = currTime + timeToCall - return id - } - - if (!window.cancelAnimationFrame) - window.cancelAnimationFrame = function(id) { - clearTimeout(id) - } - })() diff --git a/test/ssr.js b/test/ssr.js new file mode 100644 index 0000000..7a488d7 --- /dev/null +++ b/test/ssr.js @@ -0,0 +1,26 @@ +// Simple smoke-test to make sure that SSR is working correctly +const ReactDOMServer = require('react-dom/server') +const React = require('react') +const { Picker, NimblePicker } = require('../dist/index.js') +const data = require('../data/all.json') + +function testPicker() { + const element = React.createElement(Picker) + const string = ReactDOMServer.renderToString(element) + + if (typeof string !== 'string') { + throw new Error('expected a string') + } +} + +function testNimblePicker() { + const element = React.createElement(NimblePicker, { data }) + const string = ReactDOMServer.renderToString(element) + + if (typeof string !== 'string') { + throw new Error('expected a string') + } +} + +testPicker() +testNimblePicker()