<commit_before>import FineUploaderTraditional from 'src/wrappers/traditional'

const sampleBlob = new Blob(['hi!'], { type : 'text/plain' })
const sampleBlobWrapper = { blob: sampleBlob, name: 'test' }

describe('FineUploaderTraditional', () => {
    describe('new instance consruction', () => {
        it('provides access to its API', () => {
            const wrapper = new FineUploaderTraditional({
                options: {}
            })

            expect(wrapper.methods).toBeTruthy()
            expect(wrapper.methods.getFile).toBeTruthy()
            expect(wrapper.methods.addFiles).toBeTruthy()
        })

        it('provides access to passed options', () => {
            const wrapper = new FineUploaderTraditional({
                options: {
                    request: {
                        endpoint: 'foo/bar'
                    },
                    callbacks: {}
                }
            })

            expect(wrapper.options).toBeTruthy()
            expect(wrapper.options).toEqual({
                request: {
                    endpoint: 'foo/bar'
                }
            })
        })

        it('associates passed callbacks with underlying uploader instance', (done) => {
            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmit: function(id, name) {
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)
                            done()
                        }
                    }
                }
            })

            wrapper.methods.addFiles(sampleBlobWrapper)
        })
    })

    describe('callback handling', () => {
        it('associates multiple registered callback handlers w/ a single FU callback option && calls them in the order they were registered', (done) => {
            let callbacksHit = 0

            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmit: function(id, name) {
                            callbacksHit++
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)
                        }
                    }
                }
            })

            wrapper.on('onSubmit', (id, name) => {
                expect(++callbacksHit).toBe(2)
                expect(id).toBe(0)
                expect(name).toBe(sampleBlobWrapper.name)
                done()
            })

            wrapper.methods.addFiles(sampleBlobWrapper)
        })

        it('does not call subsequent registered callbacks if an earlier callback indicates failure - non-thenable', (done) => {
            let callbacksHit = 0

            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmit: function(id, name) {
                            callbacksHit++
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)
                            return false
                        }
                    }
                }
            })

            // this callback should never be executed
            wrapper.on('onSubmit', () => {
                callbacksHit++
            })

            wrapper.methods.addFiles(sampleBlobWrapper)

            setTimeout(() => {
                expect(callbacksHit).toBe(1)
                done()
            }, 500)
        })

        it('does not call subsequent registered callbacks if an earlier callback indicates failure - thenable', (done) => {
            let callbacksHit = 0

            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmit: function(id, name) {
                            callbacksHit++
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)
                            return Promise.reject()
                        }
                    }
                }
            })

            // this callback should never be executed
            wrapper.on('onSubmit', () => {
                callbacksHit++
            })

            wrapper.methods.addFiles(sampleBlobWrapper)

            setTimeout(() => {
                expect(callbacksHit).toBe(1)
                done()
            }, 500)
        })
    })
})<commit_msg>test(traditional.js): completed unit tests #1<commit_after>import qq from 'fine-uploader'

import FineUploaderTraditional from 'src/wrappers/traditional'

const sampleBlob = new Blob(['hi!'], { type : 'text/plain' })
const sampleBlobWrapper = { blob: sampleBlob, name: 'test' }

describe('FineUploaderTraditional', () => {
    describe('new instance consruction', () => {
        it('provides access to its API', () => {
            const wrapper = new FineUploaderTraditional({
                options: {}
            })

            expect(wrapper.methods).toBeTruthy()
            expect(wrapper.methods.getFile).toBeTruthy()
            expect(wrapper.methods.addFiles).toBeTruthy()
        })

        it('provides access to passed options', () => {
            const wrapper = new FineUploaderTraditional({
                options: {
                    request: {
                        endpoint: 'foo/bar'
                    },
                    callbacks: {}
                }
            })

            expect(wrapper.options).toBeTruthy()
            expect(wrapper.options).toEqual({
                request: {
                    endpoint: 'foo/bar'
                }
            })
        })

        it('associates passed callbacks with underlying uploader instance', done => {
            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmit: function(id, name) {
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)

                            done()
                        }
                    }
                }
            })

            wrapper.methods.addFiles(sampleBlobWrapper)
        })
    })

    describe('callback handling', () => {
        it('associates multiple registered callback handlers w/ a single FU callback option && calls them in the order they were registered', done => {
            let callbacksHit = 0

            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmit: function(id, name) {
                            callbacksHit++
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)
                        }
                    }
                }
            })

            wrapper.on('onSubmit', (id, name) => {
                const records = wrapper.methods.getUploads()
                expect(records.length).toBe(1)
                expect(records[0].status).toBe(qq.status.SUBMITTING)

                expect(++callbacksHit).toBe(2)
                expect(id).toBe(0)
                expect(name).toBe(sampleBlobWrapper.name)
            })

            setTimeout(() => {
                const records = wrapper.methods.getUploads()
                expect(records[0].status).toBe(qq.status.SUBMITTED)

                done()
            }, 100)

            wrapper.methods.addFiles(sampleBlobWrapper)
        })

        it('does not call subsequent registered non-thenable callbacks if an earlier callback indicates failure w/ `false`', done => {
            let callbacksHit = 0

            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmitted: function(id, name) {
                            callbacksHit++
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)
                            return false
                        }
                    }
                }
            })

            // this callback should never be executed
            wrapper.on('onSubmitted', () => {
                callbacksHit++
            })

            wrapper.methods.addFiles(sampleBlobWrapper)

            setTimeout(() => {
                expect(callbacksHit).toBe(1)

                done()
            }, 100)
        })

        it('does not call subsequent registered thenable callbacks if an earlier callback indicates failure w/ a Promise', done => {
            let callbacksHit = 0

            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmit: function(id, name) {
                            callbacksHit++
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)
                            return false
                        }
                    }
                }
            })

            // this callback should never be executed
            wrapper.on('onSubmit', () => {
                callbacksHit++
            })

            wrapper.methods.addFiles(sampleBlobWrapper)

            setTimeout(() => {
                const records = wrapper.methods.getUploads()
                expect(records.length).toBe(1)
                expect(records[0].status).toBe(qq.status.REJECTED)

                expect(callbacksHit).toBe(1)

                done()
            }, 100)
        })

        it('does not call subsequent registered thenable callbacks if an earlier callback indicates failure w/ `false`', done => {
            let callbacksHit = 0

            const wrapper = new FineUploaderTraditional({
                options: {
                    autoUpload: false,
                    callbacks: {
                        onSubmit: function(id, name) {
                            callbacksHit++
                            expect(id).toBe(0)
                            expect(name).toBe(sampleBlobWrapper.name)
                            return Promise.reject()
                        }
                    }
                }
            })

            // this callback should never be executed
            wrapper.on('onSubmit', () => {
                callbacksHit++
            })

            wrapper.methods.addFiles(sampleBlobWrapper)

            setTimeout(() => {
                const records = wrapper.methods.getUploads()
                expect(records.length).toBe(1)
                expect(records[0].status).toBe(qq.status.REJECTED)

                expect(callbacksHit).toBe(1)

                done()
            }, 100)
        })

        it('allows callback handlers to be removed', done => {
            const handler = () => callbacksHit++
            let callbacksHit = 0

            const wrapper = new FineUploaderTraditional({
                options: { autoUpload: false }
            })

            wrapper.on('onSubmit', handler)
            wrapper.methods.addFiles(sampleBlobWrapper)

            setTimeout(() => {
                expect(callbacksHit).toBe(1)

                wrapper.off('onSubmit', handler)
                wrapper.methods.addFiles(sampleBlobWrapper)

                setTimeout(() => {
                    expect(callbacksHit).toBe(1)

                    done()
                }, 100)
            }, 100)
        })
    })
})
{'fraction_non_alphanumeric': 0.09566741798146756, 'fraction_numerical': 0.005008765339343852, 'mean_word_length': 1.1385219564441271, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '789566', 'n_tokens_mistral': 2859, 'n_tokens_neox': 2618, 'n_words': 543}