Categories
discuss

‘globalThis’ underfined for Jest test cases

I am using globalThis property specifically globalThis.scrollTo(0,0) in my React WebApp.

I am using Jest for unit testing alongwith Enzyme.

As of test cases fail as it is unable to identify globalThis and says that ‘globalThis’ is undefined.

Is there a way to introduce globalThis into the tests just like jsdom does for window etc ?

For Example

— abc.tsx —

const abc: React.FC<CustomProps> = props => {
useEffect(() => {
globalThis?.scrollTo(0,0);
}
}

— abcTest.tsx —

wrapper = mount(<abc/>);

mount produces error that “globalThis” is undefined

Answer

globalThis needs node version 12+. I use n as node version management.

console.log(globalThis);

For node/10.16.2, got error:

console.log(globalThis);
            ^

ReferenceError: globalThis is not defined

For node/12.6.1, got:

Object [global] {
  global: [Circular],
  clearInterval: [Function: clearInterval],
  clearTimeout: [Function: clearTimeout],
  setInterval: [Function: setInterval],
  setTimeout: [Function: setTimeout] { [Symbol(util.promisify.custom)]: [Function] },
  queueMicrotask: [Function: queueMicrotask],
  clearImmediate: [Function: clearImmediate],
  setImmediate: [Function: setImmediate] {
    [Symbol(util.promisify.custom)]: [Function]
  }
}
Categories
discuss

Use an Oracle clob in a predicate created from a String > 4k

I’m trying to create a clob from a string of > 4000 chars (supplied in the file_data bind variable) to be used in a Oracle SELECT predicate below:

myQuery=
select *
from dcr_mols
WHERE flexmatch(ctab,:file_data,'MATCH=ALL')=1;

If I add TO_CLOB() round file_data it fails the infamous Oracle 4k limit for a varchar (it’s fine for <4k strings). The error (in SQL Developer) is:

ORA-01460: unimplemented or unreasonable conversion requested
01460. 00000 -  "unimplemented or unreasonable conversion requested"

FYI The flexmatch function is used for searching molecules , and is a described here : http://help.accelrysonline.com/ulm/onelab/1.0/content/ulm_pdfs/direct/developers/direct_2016_developersguide.pdf

The function itself is a bit complicated but the essence is the 2nd parameter has to be a clob. So my question is how do I convert a Java String bind_variable of over 4000 chars to a clob in sql (or Java).

I tried the method below (which works when inserting clobs) in Java (Spring boot 2) using:

MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("file_data", fileDataStr,Types.CLOB);
jdbcNamedParameterTemplate.query(myQuery,parameters,…

This method should work but it fails with a converluted flexmatch error which FYI is:

SQL state [99999]; error code [29902]; ORA-29902: error in executing ODCIIndexStart() routinenORA-20100: 
MDL-0203: Unable to read from CLOB (csfrm=1, csid=873): 
ORA-22922: nonexistent LOB valuenMDL-0021: Unable to copy LOB to stringnMDL-1051: Molstructure search query is not a valid moleculenMDL-0976: 
Molecule index search initialization failednORA-06512: at "C$MDLICHEM80.MDL_MXIXMDL", line 329nORA-06512: at "C$MDLICHEM80.MDL_MXIXMDL", line 309n; nested exception is java.sql.SQLException: 
ORA-29902: error in executing ODCIIndexStart() routinenORA-20100: MDL-0203: Unable to read from CLOB (csfrm=1, csid=873): 
ORA-22922: nonexistent LOB valuenMDL-0021: Unable to copy LOB to stringnMDL-1051: Molstructure search query is not a valid moleculenMDL-0976: 
Molecule index search initialization failednORA-06512: at "C$MDLICHEM80.MDL_MXIXMDL", line 329nORA-06512: at "C$MDLICHEM80.MDL_MXIXMDL", line 309n"

Note i’m using SpringBoot 2 but I can’t get any method using an OracleConnection (obtained from my Spring NamedParametersJdbcTemplate object) to work (even on clobs <4k) , so I suspect i’ve done something stupid. I’ve tried:

 @Autowired
 NamedParameterJdbcTemplate  jdbcNamedParameterTemplate;
OracleConnection conn =  this.jdbcNamedParameterTemplate.getJdbcTemplate().getDataSource().getConnection().unwrap(OracleConnection.class);
Clob myClob =  conn.createClob();
myClob.setString( 1, fileDataStr);
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("file_data", myClob,Types.CLOB);

application.properties :

spring.datasource.url=jdbc:oracle:thin:@//${ORA_HOST}:${ORA_PORT}/${ORA_SID}
spring.datasource.username=${ORA_USER}
spring.datasource.password=${ORA_PASS}

Note it works fine if I go old school and use a non spring connection plus a PreparedStatement,which has a setClob() method:

OracleDataSource ods = new OracleDataSource();
String url ="jdbc:oracle:thin:@//" + ORA_HOST +":"+ORA_PORT +"/"+ORA_SID;
ods.setURL(url);
ods.setUser(user);
ods.setPassword(passwd);
Connection conn = ods.getConnection();
Clob myClob=conn.createClob();
PreparedStatement ps = conn.prepareStatement("select dcr_number from dcr_mols WHERE flexmatch(ctab,?,'MATCH=ALL')=1");
myClob.setString(1,myMol);
ps.setClob(1,myClob);
ResultSet rs =ps.executeQuery();

But I would prefer a Spring 2 solution in Java or Sql . Any help, suggestions appreciated.

Answer

I had to revert to using a PreparedStatement, but i’ve improved the normal implementation a bit by getting the connection from Spring and using apache commons BeanListHandler to map the ResultSet to an object List

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

@Autowired
DataSource dataSource;

List<MyDao> myMethod(String fileData){
    String myQuery="select * from dcr_mols WHERE flexmatch(ctab,?,'MATCH=ALL')=1";

try {
    Connection conn = this.dataSource.getConnection()   // Get connection from spring

    Clob myClob =  conn.createClob();   // Open a dB clob 
    myClob.setString( 1, fileData);     // add data to clob
    PreparedStatement ps = conn.prepareStatement(myQuery);
    ps.setClob(1,myClob);              // Add a clob into the PreparedStatement
    ResultSet rs =ps.executeQuery();   // Execute the prepared statement

    //ResultSetHandler<List<MyDao>> handler = new BeanListHandler<MyDao>(MyDao.class);   // Define the ResultSet handler
    ResultSetHandler<List<MyDao>> handler = new BeanListHandler<MyDao>(MyDao.class, new BasicRowProcessor(new GenerousBeanProcessor()));  // This is better than the above handler , because GenerousBeanProcessor removes the requirement for the column names to exactly match the java variables

    List<MyDao> myDaoList = handler.handle(rs);   // Map ResultSet to List of MyDao objects
    }catch (Exception e) {
        e.printStackTrace();
    }

return myDaoList;
}
Categories
discuss

MediaRecorder ‘ondataavailable’ event not firing

Here’ my complete code for displaying and live recording audio and video (and later uploading the blob chunk into the server):

$(function () {
    var handleSuccess = function(stream) {

        var player = document.querySelector("#vid-user");
        player.srcObject = stream;

        console.log("Starting media recording")
        var options = {mimeType: 'video/webm'};
        var recordedChunks = [];
        var mediaRecorder = new MediaRecorder(stream, options);

        mediaRecorder.ondataavailable = function(e) {
            console.log("Data available")
            if (e.data.size > 0) {
                recordedChunks.push(e.data);
                var url = URL.createObjectURL(new Blob(recordedChunks));
                console.log("URL: " + url)
            }
        }

        mediaRecorder.start();
    };
    navigator.mediaDevices.getUserMedia({ audio: true, video: true })
        .then(handleSuccess)
})

The video playback works, but the problem is that the mediaRecorder.ondataavailable is not triggered/called. What could be the problem here?

Answer

The start() method takes an optional parameter called timeslice. Unless you specify that parameter the dataavailable event only fires after you call stop() on the MediaRecorder.

https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/start

Categories
discuss

Google Chrome Inspector on Remote Devices not working

I’m working on an Android App that uses WebView and wanted to use the inspector in order to check some things.
The problem is the elements are not visible or other tabs seem not be working, they are blank.

Does somebody else experience this issue? or is there a workaround rather than downgrading the chrome version on your system?

Since Google Chrome updated to version 80 the panel for Remote Devices has been deprecated and it’s required to go on: chrome://inspect/devices#devices
(Note: Inspector on Remote Devices was working fine on Chrome 78-79)

enter image description here

Answer

For Chrome 80 to work on the emulator is required to have an API > 26
This is required because the WebView version will be updated based on the emulator API and Chrome 80 removed support for HTML imports and (very) old versions of DevTools do not have the polyfill built in to prevent crashing. (link for open chromium bug)

API 26 has 58.0.3029 WebView version – works for older versions of Chrome < 80
API 28 has 69.0.3497.100 WebView version – works on Chrome 80

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..