Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit a1b398b

Browse files
committed
Add test cases #45
1 parent 23eee76 commit a1b398b

File tree

1 file changed

+94
-67
lines changed

1 file changed

+94
-67
lines changed

test/test-0.6.2.js

Lines changed: 94 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,23 @@ const describe = RNTest.config({
2121
expand : false,
2222
timeout : 12000,
2323
})
24-
const { TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
24+
const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
2525
const dirs = RNFetchBlob.fs.dirs
2626

2727
let prefix = ((Platform.OS === 'android') ? 'file://' : '')
28+
let photo = null
2829

2930
describe('upload asset from camera roll', (report, done) => {
3031
let imgName = `image-from-camera-roll-${Platform.OS}.jpg`
3132
CameraRoll.getPhotos({first : 10})
3233
.then((resp) => {
3334
let url = resp.edges[0].node.image.uri
34-
return fs.readFile(url, 'base64')
35-
})
36-
.then((data) => {
35+
photo = url
3736
return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
3837
Authorization : `Bearer ${DROPBOX_TOKEN}`,
3938
'Dropbox-API-Arg': `{\"path\": \"/rn-upload/${imgName}\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}`,
4039
'Content-Type' : 'application/octet-stream',
41-
}, data)
40+
}, RNFetchBlob.wrap(url))
4241
})
4342
.then((resp) => {
4443
resp = resp.json()
@@ -47,76 +46,104 @@ describe('upload asset from camera roll', (report, done) => {
4746
)
4847
done()
4948
})
50-
5149
})
5250

53-
describe('access assets from camera roll', (report, done) => {
54-
let photo = null
55-
CameraRoll.getPhotos({first : 10})
51+
describe('Upload multipart data with file from CameraRoll', (report, done) => {
52+
let filename = 'test-from-storage-img-'+Date.now()+'.png'
53+
RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
54+
'Content-Type' : 'multipart/form-data',
55+
}, [
56+
{ name : 'test-img', filename : filename, data: RNFetchBlob.wrap(photo)},
57+
{ name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
58+
{ name : 'field1', data : 'hello !!'},
59+
{ name : 'field2', data : 'hello2 !!'}
60+
])
5661
.then((resp) => {
57-
photo = resp.edges[0].node.image.uri
58-
report(<Info key="items">
59-
<Text>{photo}</Text>
60-
</Info>)
61-
return fs.readFile(photo, 'base64')
62+
resp = resp.json()
63+
report(
64+
<Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
65+
<Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
66+
)
67+
return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`)
6268
})
63-
.then((data) => {
64-
report(<Info key="asset image">
69+
.then((resp) => {
70+
report(<Info key="uploaded image">
6571
<Image
6672
style={styles.image}
67-
source={{uri: `data:image/png;base64, ${data}`}}/>
73+
source={{ uri : 'data:image/png;base64, '+ resp.base64()}}/>
6874
</Info>)
6975
done()
7076
})
7177
})
7278

73-
describe('read asset in app bundle',(report, done) => {
74-
let target = fs.asset('test-asset2.png')
75-
fs.readFile(target, 'base64')
76-
.then((data) => {
77-
report(<Info key="asset image">
78-
<Image
79-
style={styles.image}
80-
source={{uri: `data:image/png;base64, ${data}`}}/>
81-
</Info>)
82-
return fs.readFile(fs.asset('test-asset1.json'), 'utf8')
83-
})
84-
.then((resp) => {
85-
report(
86-
<Assert key="asset content verify"
87-
expect="asset#1"
88-
actual={JSON.parse(resp).secret}/>)
89-
done()
90-
})
91-
})
92-
93-
describe('stat assets in app', (report, done) => {
94-
fs.stat(fs.asset('test-asset2.png'))
95-
.then((data) => {
96-
report(<Info key="list of assets">
97-
<Text>{JSON.stringify(data)}</Text>
98-
</Info>)
99-
done()
100-
})
101-
})
102-
103-
describe('copy asset', (report, done) => {
104-
let dest = `${dirs.DocumentDir}/test-asset-1-${Date.now()}.json`
105-
fs.cp(fs.asset('test-asset1.json'), dest)
106-
.then(() => fs.readFile(dest, 'utf8'))
107-
.then((data) => {
108-
report(<Assert key="asset copied correctly"
109-
expect={'asset#1'}
110-
actual={JSON.parse(data).secret}/>)
111-
return fs.stat(fs.asset('test-asset1.json'))
112-
})
113-
.then((stat) => {
114-
report(<Assert key="file size check"
115-
expect={27}
116-
actual={Math.floor(stat.size)}/>,
117-
<Info key="dest file info">
118-
<Text>{JSON.stringify(stat)}</Text>
119-
</Info>)
120-
done()
121-
})
122-
})
79+
//
80+
// describe('access assets from camera roll', (report, done) => {
81+
// let photo = null
82+
// CameraRoll.getPhotos({first : 10})
83+
// .then((resp) => {
84+
// photo = resp.edges[0].node.image.uri
85+
// report(<Info key="items">
86+
// <Text>{photo}</Text>
87+
// </Info>)
88+
// return fs.readFile(photo, 'base64')
89+
// })
90+
// .then((data) => {
91+
// report(<Info key="asset image">
92+
// <Image
93+
// style={styles.image}
94+
// source={{uri: `data:image/png;base64, ${data}`}}/>
95+
// </Info>)
96+
// done()
97+
// })
98+
// })
99+
//
100+
// describe('read asset in app bundle',(report, done) => {
101+
// let target = fs.asset('test-asset2.png')
102+
// fs.readFile(target, 'base64')
103+
// .then((data) => {
104+
// report(<Info key="asset image">
105+
// <Image
106+
// style={styles.image}
107+
// source={{uri: `data:image/png;base64, ${data}`}}/>
108+
// </Info>)
109+
// return fs.readFile(fs.asset('test-asset1.json'), 'utf8')
110+
// })
111+
// .then((resp) => {
112+
// report(
113+
// <Assert key="asset content verify"
114+
// expect="asset#1"
115+
// actual={JSON.parse(resp).secret}/>)
116+
// done()
117+
// })
118+
// })
119+
//
120+
// describe('stat assets in app', (report, done) => {
121+
// fs.stat(fs.asset('test-asset2.png'))
122+
// .then((data) => {
123+
// report(<Info key="list of assets">
124+
// <Text>{JSON.stringify(data)}</Text>
125+
// </Info>)
126+
// done()
127+
// })
128+
// })
129+
//
130+
// describe('copy asset', (report, done) => {
131+
// let dest = `${dirs.DocumentDir}/test-asset-1-${Date.now()}.json`
132+
// fs.cp(fs.asset('test-asset1.json'), dest)
133+
// .then(() => fs.readFile(dest, 'utf8'))
134+
// .then((data) => {
135+
// report(<Assert key="asset copied correctly"
136+
// expect={'asset#1'}
137+
// actual={JSON.parse(data).secret}/>)
138+
// return fs.stat(fs.asset('test-asset1.json'))
139+
// })
140+
// .then((stat) => {
141+
// report(<Assert key="file size check"
142+
// expect={27}
143+
// actual={Math.floor(stat.size)}/>,
144+
// <Info key="dest file info">
145+
// <Text>{JSON.stringify(stat)}</Text>
146+
// </Info>)
147+
// done()
148+
// })
149+
// })

0 commit comments

Comments
 (0)