aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/utilsSpec.js
diff options
context:
space:
mode:
authorMatthew Shao <me@matshao.com>2017-04-26 17:06:50 +0800
committerMatthew Shao <me@matshao.com>2017-04-26 17:06:50 +0800
commitec7d90f9be1dca8a989da2696df1bba223c01364 (patch)
treea17f4b79a0d62565d92be018e8a756135ba29788 /web/src/js/__tests__/utilsSpec.js
parente32efcae49ba5857feae85b9b4651a45d9e5fcc3 (diff)
downloadmitmproxy-ec7d90f9be1dca8a989da2696df1bba223c01364.tar.gz
mitmproxy-ec7d90f9be1dca8a989da2696df1bba223c01364.tar.bz2
mitmproxy-ec7d90f9be1dca8a989da2696df1bba223c01364.zip
[web] Add coverage for js/utils.js, js/flow/utils.js
Diffstat (limited to 'web/src/js/__tests__/utilsSpec.js')
-rw-r--r--web/src/js/__tests__/utilsSpec.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/web/src/js/__tests__/utilsSpec.js b/web/src/js/__tests__/utilsSpec.js
new file mode 100644
index 00000000..9a1a0750
--- /dev/null
+++ b/web/src/js/__tests__/utilsSpec.js
@@ -0,0 +1,95 @@
+import * as utils from '../utils'
+
+global.fetch = jest.fn()
+
+describe('formatSize', () => {
+ it('should return 0 when 0 byte', () => {
+ expect(utils.formatSize(0)).toEqual('0')
+ })
+
+ it('should return formatted size', () => {
+ expect(utils.formatSize(27104011)).toEqual("25.8mb")
+ expect(utils.formatSize(1023)).toEqual("1023b")
+ })
+})
+
+describe('formatTimeDelta', () => {
+ it('should return formatted time', () => {
+ expect(utils.formatTimeDelta(3600100)).toEqual("1h")
+ })
+})
+
+describe('formatTimeSTamp', () => {
+ it('should return formatted time', () => {
+ expect(utils.formatTimeStamp(1483228800)).toEqual("2017-01-01 00:00:00.000")
+ })
+})
+
+describe('reverseString', () => {
+ it('should return reversed string', () => {
+ let str1 = "abc", str2="xyz"
+ expect(utils.reverseString(str1) > utils.reverseString(str2)).toBeTruthy()
+ })
+})
+
+describe('fetchApi', () => {
+ it('should handle fetch operation', () => {
+ utils.fetchApi('http://foo/bar', {method: "POST"})
+ expect(fetch.mock.calls[0][0]).toEqual(
+ "http://foo/bar?_xsrf=undefined"
+ )
+ fetch.mockClear()
+
+ utils.fetchApi('http://foo?bar=1', {method: "POST"})
+ expect(fetch.mock.calls[0][0]).toEqual(
+ "http://foo?bar=1&_xsrf=undefined"
+ )
+
+ })
+
+ it('should be possible to do put request', () => {
+ fetch.mockClear()
+ utils.fetchApi.put("http://foo", [1, 2, 3], {})
+ expect(fetch.mock.calls[0]).toEqual(
+ [
+ "http://foo?_xsrf=undefined",
+ {
+ body: "[1,2,3]",
+ credentials: "same-origin",
+ headers: { "Content-Type": "application/json" },
+ method: "PUT"
+ },
+ ]
+ )
+ })
+})
+
+describe('getDiff', () => {
+ it('should return json object including only the changed keys value pairs', () => {
+ let obj1 = {a: 1, b:{ foo: 1} , c: [3]},
+ obj2 = {a: 1, b:{ foo: 2} , c: [4]}
+ expect(utils.getDiff(obj1, obj2)).toEqual({ b: {foo: 2}, c:[4]})
+ })
+})
+
+describe('pure', () => {
+ let tFunc = function({ className }) {
+ return (<p className={ className }>foo</p>)
+ },
+ puredFunc = utils.pure(tFunc),
+ f = new puredFunc('bar')
+
+ it('should display function name', () => {
+ expect(utils.pure(tFunc).displayName).toEqual('tFunc')
+ })
+
+ it('should suggest when should component update', () => {
+ expect(f.shouldComponentUpdate('foo')).toBeTruthy()
+ expect(f.shouldComponentUpdate('bar')).toBeFalsy()
+ })
+
+ it('should render properties', () => {
+ expect(f.render()).toEqual(tFunc('bar'))
+ })
+
+})